android - Retrofit2如何解析带有内联标签的xml

标签 android xml-parsing retrofit2

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

但是,我从未遇到过这些内联(?)标签。 api 返回一些内容,但响应正文为空。

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

我在谷歌上搜索过这个问题,但找不到有效的解决方案。请帮忙!

最佳答案

我建议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;

        // ....
}  

关于android - Retrofit2如何解析带有内联标签的xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56642404/

相关文章:

android - ViewPager getItemPosition() 从未调用过。谁应该调用它?

c++ - 取代ezXML

android - 改造 : How to make XML request and get back JSON response

android - 我如何在 Android 中发布 json 数据

android - 微调器下拉高度以匹配父项

android - 使用 Eclipse 进行 USB 调试

java - Volley NetworkImageView - 如何回收位图?

python - 使用 ElementTree 时出现未定义实体错误

android - 带有大 XML 的 Jsoup

android - 在 Retrofit2 中的单个请求中使用 @FieldMap 和 @Part 会导致方法出现 java.lang.IllegalArgumentException : Only one encoding annotation is allowed.