Java 如何解析XML并将数据输入RecyclerView?
我的应用程序目前从一个站点提取一些API数据。我需要它来显示RecyclerView列表中的元素 API数据如下所示:Java 如何解析XML并将数据输入RecyclerView?,java,android,xml-parsing,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>
<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>
54397325
6.
21
2.
4.10
33584556
头先安卓开发:大脑友好型指南
773952
黎明格里菲斯
https://images.gr-assets.com/books/1492905802m/33584556.jpg
https://images.gr-assets.com/books/1492905802s/33584556.jpg
我需要书名、作者和图像URL在RecyclerView中列出它们。按照此链接解析xml 请记住,如果在应用程序中有字符串格式的xml文本,则需要将xml字符串转换为如下所示的inputStream对象
InputStream stream = new ByteArrayInputStream(exampleXMLString.getBytes(StandardCharsets.UTF_8));
您正在使用改型来进行API调用吗?检查此链接以解析xml@giulioptenuzzo在您提供的链接中,他们使用了
InputStream is=getAssets().open(“file.xml”)代码>。我如何更改它,以便它从API请求中读取XML?您阅读了吗?我发布了一个答案,向您展示如何转换InputStream中的字符串,假设您的xml是字符串格式的