Skip to content

Instantly share code, notes, and snippets.

@melwil
Created March 1, 2012 01:35
Show Gist options
  • Save melwil/1946482 to your computer and use it in GitHub Desktop.
Save melwil/1946482 to your computer and use it in GitHub Desktop.
artist umarshalling spotify
XML from spotify API
------------------
<?xml version="1.0" encoding="iso-8859-1"?>
<artist xmlns="http://www.spotify.com/ns/music/1">
<name>David Guetta</name>
</artist>
------------------
Artist.java;
------------------
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlAccessorType(XmlAccessType.NONE)
@XmlRootElement(name = "artist")
public class Artist {
/*
XML exdample for artist
<?xml version="1.0" encoding="iso-8859-1"?>
<artist xmlns="http://www.spotify.com/ns/music/1">
<name>David Guetta</name>
</artist>
*/
@XmlElement(name = "name")
private String name;
public Artist() {
}
public String getName() {
return name;
}
public void setName(String name) {
System.out.println("Setting name.");
this.name = name;
}
}
------------------
The code;
------------------
Artist artist = null;
try {
JAXBContext jc = JAXBContext.newInstance(Artist.class);
Unmarshaller u = jc.createUnmarshaller();
artist = u.unmarshal(new StreamSource(file), Artist.class).getValue();
} catch (JAXBException e) {
e.printStackTrace();
}
if (artist != null) {
System.out.println("Artist name; "+artist.getName());
}
------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment