Retrofit2如何使用内联标签解析xml

时间:2019-06-18 05:44:47

标签: android xml-parsing retrofit2

我正在使用retrofit2和简单的xml解析器将xml值解析为类。

但是,我从未遇到过这些inline(?)标记。 api返回了一些信息,但响应的主体为null。

xml如下所示。

<?xml version="1.0" encoding="ISO-8859-1"?>
<Book title="Title Name" ver="20190513001" coverimg="PPO01.jpg" bookcode="PPO01">
    <chapters count="1">
        <chapter endflag="000" thumbnail="PPO0101_TN.jpg" chaptercode="PPO0101" num="1">
        <chaptername>ChapterName</chaptername>

        <movies count="1">

            <movie num="1" name="Movie Name" url="Some Url" type="AR">
                <directions count="0"/>

            </movie>
        </movies>
    </chapter>
</chapters>
</Book>

我对此的响应代码如下。

根对象。

@Root(name = "Book", strict = false)
public class XmlBook implements Serializable {

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

    @Element(name = "ver")
    private String ver;

    @Element(name = "coverimg")
    private String coverimg;

    @Element(name = "bookcode")
    private String bookcode;

    @Element(name = "chapters")
    private XmlChapterHeader chapters;
}

“章节”对象。

@Root(name = "chapters", strict = false)
public class XmlChapterHeader {

    @Element(name = "count")
    private int count;

    @ElementList(name = "chapter", inline = true)
    private List<XmlChapter> chapter;

}

“章”对象。

@Root(name = "chapter", strict = false)
public class XmlChapter {

    @Element(name = "num")
    private String num;

    @Element(name = "chaptercode")
    private String chaptercode;

    @Element(name = "thumbnail")
    private String thumbnail;

    @Element(name = "endflag")
    private String endflag;


    @Element(name = "chaptername")
    private String chaptername;


    @Element(name = "movies")
    private XmlMovieContainer movies;
}

“电影”对象。

@Root(name = "movies", strict = false)
public class XmlMovieContainer {

    @ElementList(name = "movies", inline = true)
    List<XmlMovie> movie;
}

最后,“电影”对象

@Root(name = "movie", strict = false)
public class XmlMovie {

    @Element(name = "directions")
    private int directions;
}

我已经在Google上搜索了此问题,但找不到有效的解决方案。请帮忙!

2 个答案:

答案 0 :(得分:1)

我建议使用TikXml进行xml解析,因为它比其他xml解析器更易于使用并且非常快速。

您必须为tikxml添加gradle插件

implementation 'com.tickaroo.tikxml:annotation:0.8.13'
implementation 'com.tickaroo.tikxml:core:0.8.13'
implementation 'com.tickaroo.tikxml:retrofit-converter:0.8.13'

改装配置:

TikXml tikxml = new TikXml.Builder().exceptionOnUnreadXml(false).build();
Retrofit retrofit =new  Retrofit.Builder()
                .baseUrl("base url")
                .addConverterFactory(TikXmlConverterFactory.create(tikxml))
                .build();

您必须使用tikxml注释创建pojo类

import com.tickaroo.tikxml.annotation.Attribute;
import com.tickaroo.tikxml.annotation.PropertyElement;
import com.tickaroo.tikxml.annotation.Xml;

import org.simpleframework.xml.Element;

import java.util.List;

@Xml
public class XmlBook {
    @Attribute(name = "title")
    String title;

    @Attribute(name = "ver")
    String ver;

    @Attribute(name = "coverimg")
    String coverimg;

    @Attribute(name = "bookcode")
    String bookcode;


    @Element(name = "chapters")
    Chapters chapters;
}
    @Xml
    public class Chapters {

        @Attribute(name = "count")
        String count;

        List<Chapter> chapters;

    }

    @Xml(name = "chapter")
    class Chapter {
        @PropertyElement(name = "chaptername")
        String chaptername;

        // ....
}  

答案 1 :(得分:0)

我认为您应该了解“ XML元素”和“ XML属性”之间的区别。

http://simple.sourceforge.net/download/stream/doc/tutorial/tutorial.php