我正在使用 JAXB 2.x 来解组 RSS 提要。但是,我不知道如何从元素中提取属性。XML(删除了不重要的部分)如下所示:
<rss>
<channel>
<item>
<title>TWiT 417: Stop Saying Ball Pit</title>
<description>Description</Description>
<pubDate>Mon, 05 Aug 2013 16:29:52 -0000</pubDate>
<enclosure url="http://www.podtrac.com/pts/redirect.mp3/aolradio.podcast.aol.com/twit/twit0417b.mp3" length="18366464" type="audio/mpeg"/>
<media:content url="http://www.podtrac.com/pts/redirect.mp3/aolradio.podcast.aol.com/twit/twit0417b.mp3" medium="audio" type="audio/mpeg" filesize="56888697"/>
</item>
我有一个 RSS 课程:
@XmlRootElement(name="rss")
public class Rss {
@XmlElementWrapper( name = "channel" )
@XmlElement( name = "item" )
// Either of the next two work
// public Item[] item;
public ArrayList< Item > items;
public String toString() {
return ReflectionToStringBuilder.toString( this );
}
}
还有一个 Item 类:
public class Item {
@XmlElement
String title;
@XmlElement
String pubDate;
@XmlElement
String guid;
@XmlElement
String link;
@XmlAttribute
String url;
public String toString() {
return ReflectionToStringBuilder.toString( this );
}
}
但是在我解组之后,enclosure/@url 始终为空。另外,我不确定如何区分enclosure/@url 和media:content/@url。我究竟做错了什么?