Retrofit 2如何在没有根元素的情况下解析XML

时间:2016-11-13 16:03:13

标签: java android xml-parsing retrofit2 simple-xml-converter

这是我要解析的响应:

<paste>
<paste_key>UW369pYh</paste_key>
<paste_date>1478837545</paste_date>
<paste_title>10/11/2016 - KTOS</paste_title>
<paste_size>16201</paste_size>
<paste_expire_date>0</paste_expire_date>
<paste_private>0</paste_private>
<paste_format_short>text</paste_format_short>
<paste_format_long>None</paste_format_long>
<paste_url>http://pastebin.com/UW369pYh</paste_url>
<paste_hits>5869</paste_hits>
</paste>
<paste>
<paste_key>NqQTQeYj</paste_key>
<paste_date>1478968384</paste_date>
<paste_title>Buffs/Nerfs de las megas en S&amp;M</paste_title>
<paste_size>2232</paste_size>
<paste_expire_date>0</paste_expire_date>
<paste_private>0</paste_private>
<paste_format_short>text</paste_format_short>
<paste_format_long>None</paste_format_long>
<paste_url>http://pastebin.com/NqQTQeYj</paste_url>
<paste_hits>589</paste_hits>
</paste>
<paste>
<paste_key>xnrqJF59</paste_key>
<paste_date>1478849206</paste_date>
<paste_title>Lista_Mario_PT.m3u</paste_title>
<paste_size>6079</paste_size>
<paste_expire_date>0</paste_expire_date>
<paste_private>0</paste_private>
<paste_format_short>cpp</paste_format_short>
<paste_format_long>C++</paste_format_long>
<paste_url>http://pastebin.com/xnrqJF59</paste_url>
<paste_hits>1928</paste_hits>
</paste>
<paste>
<paste_key>RHrCmtpG</paste_key>
<paste_date>1478717113</paste_date>
<paste_title>Rigged US Elections 2016</paste_title>
<paste_size>1341</paste_size>
<paste_expire_date>0</paste_expire_date>
<paste_private>0</paste_private>
<paste_format_short>text</paste_format_short>
<paste_format_long>None</paste_format_long>
<paste_url>http://pastebin.com/RHrCmtpG</paste_url>
<paste_hits>8463</paste_hits>
</paste>

如您所见,没有根元素,只有粘贴元素列表。现在我使用响应类型作为粘贴,并且只能解析响应的第一个元素。

如何将整个响应解析为List<Paste>对象?

1 个答案:

答案 0 :(得分:0)

您可以尝试使用自定义解析器来解析响应。这是一种样板解决方案。您需要编写一个通用而不是可重用的代码。检查另一个answer以获取有关Retrofit2的自定义解析器的更多详细信息。