如何解析XML并将数据输入RecyclerView?

时间:2018-07-17 12:55:29

标签: java android xml-parsing

我的应用当前正在从站点中提取一些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中列出它们。

1 个答案:

答案 0 :(得分:0)

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

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

InputStream stream = new ByteArrayInputStream(exampleXMLString.getBytes(StandardCharsets.UTF_8));
相关问题