Created
February 16, 2015 11:42
-
-
Save uttesh/54edf90f9c6fad3e525f to your computer and use it in GitHub Desktop.
Reading multiple xml documents objects from xml file by using JAXB
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to write all this contents to an XML file?