【问题标题】:How do I parse XML and feed the data into RecyclerView?如何解析 XML 并将数据输入 RecyclerView?
【发布时间】:2018-07-17 12:55:29
【问题描述】:

我的应用目前从网站中提取一些 API 数据。我需要它来显示 RecyclerView 列表中的元素。

API 数据是这样的:

<work>
    <id type="integer">54397325</id>
    <books_count type="integer">6</books_count>
    <ratings_count type="integer">21</ratings_count>
    <text_reviews_count type="integer">2</text_reviews_count>
    <original_publication_year type="integer" nil="true"/>
    <original_publication_month type="integer" nil="true"/>
    <original_publication_day type="integer" nil="true"/>
    <average_rating>4.10</average_rating>
    <best_book type="Book">
        <id type="integer">33584556</id>
        <title>
            Head First Android Development: A Brain-Friendly Guide
        </title>
        <author>
            <id type="integer">773952</id>
            <name>Dawn Griffiths</name>
        </author>
        <image_url>
            https://images.gr-assets.com/books/1492905802m/33584556.jpg
        </image_url>
        <small_image_url>
            https://images.gr-assets.com/books/1492905802s/33584556.jpg
        </small_image_url>
    </best_book>
</work>

我需要书名、作者和图片 URL 才能在 RecyclerView 中列出它们。

【问题讨论】:

  • 您是否使用 Retrofit 进行 API 调用?
  • @GiulioPettenuzzo 在您提供的链接中,他们使用了InputStream is = getAssets().open("file.xml");。如何更改它以便从 API 请求中读取 XML?
  • 你读过这个吗? Google Documentation
  • 我发布一个答案,向您展示如何在 InputStream 中转换字符串,假设您的 xml 是字符串格式

标签: java android xml-parsing


【解决方案1】:

点击此链接解析xml https://www.tutorialspoint.com/android/android_xml_parsers.htm

请记住,如果您的应用程序中有字符串格式的 xml 文本,则需要将 xml 字符串转换为如下所示的 inputStream 对象

InputStream stream = new ByteArrayInputStream(exampleXMLString.getBytes(StandardCharsets.UTF_8));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多