Question

I'm using JAXB 2.x to unmarshal a RSS feed. I don't know how to pull an attribute from an element however. The XML (unimportant parts removed) looks like:

    <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>

I have an RSS class:

@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 );
    }
}

And an Item class:

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 );
    }
}

But the enclosure/@url is is always null following my unmarshal. Also, I'm not sure how I differentiate between enclosure/@url and media:content/@url. What am I doing wrong?

Was it helpful?

Solution

Your @XmlAttribute annotation is on the Item class so it's expecting

<item url="...">

You could create another class Enclosure and move the @XmlAttribute to that class (and similar for media...)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top