Here is an example I need to deal with, it looks complicated. With the help of JAXB, it's really easy to get all values from the xml file.
For example: article.xml
<?xml version="1.0" encoding="utf-8"?>
<article_set num_found="37">
<article>
<category>
<dashed_name>world</dashed_name>
<name>World</name>
</category>
<description>A compromise deal appeared to bring a tentative peace to
Southern Weekend’s newsroom, as another Chinese newspaper faced a
crisis on Wednesday over the forced publishing of an editorial.
</description>
<title>Censored Newspaper in China Returns to Publication Amid
Struggles</title>
<created_at>2013-01-09 19:13:54</created_at>
<author_set>
<author>
<last_name>Wong</last_name>
<first_name>Edward</first_name>
<guid>2f603b4242fa0c2765b01a980a11bfed</guid>
</author>
<author>
<last_name>Ansfield</last_name>
<first_name>Jonathan</first_name>
<guid>fe51312fbcec598a57e74a2a9eaa75f0</guid>
</author>
</author_set>
<source>
<website>http://www.nytimes.com</website>
<name>The New York Times</name>
<circulation>15577816</circulation>
<country>US</country>
<company_type>Public</company_type>
<founded>1851</founded>
<staff_authors>350</staff_authors>
<frequency>Daily</frequency>
<owner>The New York Times Company</owner>
<media_type>
<dashed_name>newspaper</dashed_name>
<name>Newspaper</name>
</media_type>
<guid>1ce0362f2e764a95b0c7351c05a4eb19</guid>
<is_blog>0</is_blog>
<thumbnail>http://www.google.com/s2/favicons?domain=www.nytimes.com
</thumbnail>
<description>The New York Times is a daily newspaper published in New
York City
and distributed internationally. It is owned by The New York Times
Company, which publishes 15 other newspapers, including the
International Herald Tribune and The Boston Globe. It is the
largest metropolitan newspaper in the United States. Nicknamed
the "Gray Lady" for its staid appearance and style, it is
often
regarded as a national newspaper of record, meaning that it is
frequently relied upon as the official and authoritative reference
for modern events.
</description>
</source>
<published_at>2013-01-09 18:55:40</published_at>
<score>1.033056</score>
<link>http://www.nytimes.com/2013/01/10/world/asia/chinese-officials-pledge-to-loosen-controls-over-embattled-newspaper.html?partner=rss&emc=rss
</link>
<guid>d022bc904660cee266803e5374cb16df</guid>
<thumbnail>
<original_image>http://graphics8.nytimes.com/images/2013/01/10/world/china/china-moth.jpg
</original_image>
<link>http://images.newscred.com/Zz1mZDE4MzQyMDkxMzhjNTRmOTk4OGYzNmNkNDExYzJiYQ==?width=75&height=75
</link>
</thumbnail>
<metadata />
</article>
<article>
<category>
<dashed_name>world</dashed_name>
<name>World</name>
</category>
<description>The writer Leung Ping-kwan was a caring, intelligent
advocate of diversity in Chinese culture. In an age that saw so many
attempts to reduce the many to the one, he wrote for the many.
</description>
<title>Letter from China: Poetic Voice That Spoke for Pluralism
</title>
<created_at>2013-01-09 14:52:00</created_at>
<author_set>
<author>
<last_name>TATLOW</last_name>
<first_name>DIDI KIRSTEN</first_name>
<guid>4621adc0e5691a0d0e652e1aa99c1bce</guid>
</author>
</author_set>
<source>
<website>http://www.nytimes.com</website>
<name>The New York Times</name>
<circulation>15577816</circulation>
<country>US</country>
<company_type>Public</company_type>
<founded>1851</founded>
<staff_authors>350</staff_authors>
<frequency>Daily</frequency>
<owner>The New York Times Company</owner>
<media_type>
<dashed_name>newspaper</dashed_name>
<name>Newspaper</name>
</media_type>
<guid>1ce0362f2e764a95b0c7351c05a4eb19</guid>
<is_blog>0</is_blog>
<thumbnail>http://www.google.com/s2/favicons?domain=www.nytimes.com
</thumbnail>
<description>The New York Times is a daily newspaper published in New
York City
and distributed internationally. It is owned by The New York Times
Company, which publishes 15 other newspapers, including the
International Herald Tribune and The Boston Globe. It is the
largest metropolitan newspaper in the United States. Nicknamed
the "Gray Lady" for its staid appearance and style, it is
often
regarded as a national newspaper of record, meaning that it is
frequently relied upon as the official and authoritative reference
for modern events.
</description>
</source>
<published_at>2013-01-09 14:40:07</published_at>
<score>1.0312451</score>
<link>http://www.nytimes.com/2013/01/10/world/asia/10iht-letter10.html?partner=rss&emc=rss
</link>
<guid>3160d5d2ba7376c400f50fc9df0479cf</guid>
<metadata />
</article>
<!-- more articles -->
</article_set>
In this case, we need to start from article_set node. It only wraps article node. So In our Article_set class, we have a list of articles.
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlSeeAlso;
@XmlRootElement(name = "article_set")
@XmlSeeAlso(Article.class)
public class ArticleSet {
private List<Article> articles;
public ArticleSet() {
articles = new ArrayList<Article>();
}
@XmlElement(name = "article")
public List<Article> getArticles() {
return articles;
}
public void setArticles(List<Article> articles) {
this.articles = articles;
}
}
Then, we start on Article node, it contains a lot of child nodes, some of them are converted to Object because they need to wrap other nodes like category, source.
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlSeeAlso;
@XmlRootElement(name="article")
@XmlSeeAlso({ Category.class, AuthorSet.class, Source.class, Thumbnail.class })
public class Article {
private Category category;
private String description;
private String title;
private String created_at;
private AuthorSet author_set;
private Source source;
private String published_at;
private String score;
private String link;
private String guid;
private Thumbnail thumbnail;
private String metadata;
@XmlElement(name = "category")
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
@XmlElement
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@XmlElement
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@XmlElement
public String getCreated_at() {
return created_at;
}
public void setCreated_at(String created_at) {
this.created_at = created_at;
}
@XmlElement(name = "author_set")
public AuthorSet getAuthor_set() {
return author_set;
}
public void setAuthor_set(AuthorSet author_set) {
this.author_set = author_set;
}
@XmlElement
public Source getSource() {
return source;
}
public void setSource(Source source) {
this.source = source;
}
@XmlElement
public String getPublished_at() {
return published_at;
}
public void setPublished_at(String published_at) {
this.published_at = published_at;
}
@XmlElement
public String getScore() {
return score;
}
public void setScore(String score) {
this.score = score;
}
@XmlElement
public String getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
@XmlElement
public String getGuid() {
return guid;
}
public void setGuid(String guid) {
this.guid = guid;
}
@XmlElement
public Thumbnail getThumbnail() {
return thumbnail;
}
public void setThumbnail(Thumbnail thumbnail) {
this.thumbnail = thumbnail;
}
@XmlElement
public String getMetadata() {
return metadata;
}
public void setMetadata(String metadata) {
this.metadata = metadata;
}
}
Other nodes are similar.
Note:
1. When I use JAXB annotations, one exception is related to node has the same name. This is because I placed annotations @XMLElement on fields. The annotation should be placed on getter method. I think this might be the problem regarding to jar version.
2. I misunderstood annotation @XmlElementWrapper, If we start from Article node, we can use @XmlElementWrapper(name="article_set"). In my case, I didn't need to use this annotation for article.
I found an article to show detailed example on that. This is very useful and clear.
http://blog.bdoughan.com/2010/09/jaxb-collection-properties.html