简体   繁体   中英

Converting XML response to POJO using SimpleXMLConverter in Retrofit

I am stuck at this error below. This is the XML response

<rss xmlns:content="http://purl.org/rss/1.0/modules/content/"     xmlns:dcterms="http://purl.org/dc/terms/"  xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        <title>SnagFilms Recent Additions</title>
        <item>
            <media:title>Sink or Swim - Trailer</media:title>
            <title>Sink or Swim - Trailer</title>
            <media:description> Jon Bowermaster's documentary on a learn-to-swim camp for third graders and their moms on the island-nation of the Maldives
            </media:description>
            <media:credit role="Director" scheme="urn:ebu">Jon Bowermaster</media:credit>
            <media:thumbnail type="landscape" url="http://snagfilms-a.akamaihd.net/sinkorswim-video.jpg"/>
            <media:content duration="117" height="323"  width="500"/>
            <media:rights status="official"/>
            <media:player height="323" url="http://embed.snagfilms.com/embed/player?filmId=00000158-b20c-d8f9-affd-b32ce8700000" width="500"/>
         </item>
    </channel>
</rss>

This is my Rest Call code using an observable

rx.Observable<Rss> videosObservable = RetrofitHelper.createListObs();
    videosObservable.subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Subscriber<Rss>() {
                @Override
                public void onCompleted() {

                    Log.d(TAG, "onCompleted: ");
                }

                @Override
                public void onError(Throwable e) {
                    Log.d(TAG, "onError: " + e.toString());
                }

                @Override
                public void onNext(Rss rss) {
                    Log.d(TAG, "onNext: " );

                }
            });

This is the error I am getting

onError: java.lang.RuntimeException: org.simpleframework.xml.core.ElementException: Element 'item' does not have a match in class com.example.singh.xmlParser.model.Channel at line 12

My Rss class

@Root(strict = false)
public class Rss {

public Channel getChannel() {
    return channel;
}
@Element
public Channel channel;

}

My Channel class

@Element
public class Channel {

@Element(name = "title")
String title;


@ElementList
List<Item> items;

public List getItems(){
    return items;
}

}

I need help making proper POJOs for XML in Java. I would really appreciate if someone could make all POJOs for the given format. Response link http://www.snagfilms.com/feeds/ . Converter documentation http://simple.sourceforge.net/home.php

Your list of item s is an inline list within the channel object. You have to specify that in your annotaion:

@ElementList(inline=true)
List<Item> items;

Check the official documentation .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM