【问题标题】:Retrofit2 how to parse xml with inline tagsRetrofit2如何使用内联标签解析xml
【发布时间】:2019-10-31 16:51:08
【问题描述】:

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

但是,我从来没有遇到过这些 inline(?) 标签。 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;
}

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

【问题讨论】:

    标签: android xml-parsing retrofit2


    【解决方案1】:

    我想你应该知道“XML 元素”和“XML 属性”的区别。

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 这真的很管用,因为我使用了我现有的项目,
      猜你喜欢
      • 2013-09-30
      • 2011-02-08
      • 2013-11-19
      • 2015-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-27
      相关资源
      最近更新 更多