简体   繁体   中英

Retrofit 2 How to parse XML without a root element

This is the response that I want to parse :

<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>

As you can see, there is no root element, only a list of paste elements. Right now I am using the response type as Paste, and can only parse the first element of the response.

How can I parse the whole response into a List<Paste> object ?

You can try to use custom parsers to parse a response. It's a kind of boilerplate solution. You need to write a common and not reusable code. Check another answer for more details about custom parsers for Retrofit2.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM