Skip to content

Instantly share code, notes, and snippets.

@uttesh
Created February 16, 2015 11:42
Show Gist options
  • Save uttesh/54edf90f9c6fad3e525f to your computer and use it in GitHub Desktop.
Save uttesh/54edf90f9c6fad3e525f to your computer and use it in GitHub Desktop.
Reading multiple xml documents objects from xml file by using JAXB
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
/**
*
* @author Rivet Systems
*/
@XmlRootElement(name = "album")
@XmlAccessorType(XmlAccessType.FIELD)
public class Album {
@XmlElement(name = "title")
String title;
@XmlElement(name = "url")
String url;
@XmlAttribute(name = "id")
String id;
@XmlAttribute(name = "group")
String group;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getGroup() {
return group;
}
public void setGroup(String group) {
this.group = group;
}
}
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlList;
import javax.xml.bind.annotation.XmlRootElement;
/**
*
* @author Rivet Systems
*/
@XmlRootElement(name = "albums")
@XmlAccessorType (XmlAccessType.FIELD)
public class Files {
@XmlElement(name = "album")
List<Album> albums = new ArrayList<Album>();
public List<Album> getAlbums() {
return albums;
}
public void setAlbums(List<Album> albums) {
this.albums = albums;
}
}
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<albums>
<album id="100" group="false">
<title>title1</title>
<url>url1</url>
</album>
<album id="101" group="true">
<title>title2</title>
<url>url2</url>
</album>
<album id="102" group="true">
<title>title3</title>
<url>url3</url>
</album>
<album id="103" group="false">
<title>title4</title>
<url>url4</url>
</album>
</albums>
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
/**
*
* @author Rivet Systems
*/
public class ReadXml {
public static void main(String[] args) {
try {
File file = new File("files.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Files.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Files Files = (Files) jaxbUnmarshaller.unmarshal(file);
System.out.println("Files :"+Files);
for(Album album : Files.getAlbums()){
System.out.println("Id : "+album.getId());
System.out.println("Group : "+album.getGroup());
System.out.println("Title : "+album.getTitle());
System.out.println("getUrl : "+album.getUrl());
}
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
@makgaboemmanuel
Copy link

How to write all this contents to an XML file?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment