当前位置: 动力学知识库 > 问答 > 编程问答 >

android - how to parse an xml response with multiple name tags in it

问题描述:

Hi i am usin XML PULL PARSER for parsing my response as the response it too large about 6mb of size. The response contains multiple name tags in it. How can i parse to attain string tags seperately for respective name tags . My response is:

<?xml version="1.0" encoding="ISO-8859-1" ?>

- <methodResponse>

- <params>

- <param>

- <value>

- <array>

- <data>

- <value>

<int>7</int>

</value>

- <value>

- <struct>

- <member>

<name>AdURLs</name>

- <value>

- <struct>

- <member>

<name>small</name>

- <value>

- <array>

- <data>

- <value>

<string>http://www.jumptap.com/images/cg-windowshade-collapsed.jpg</string>

</value>

- <value>

<string>http://mobilesitelinkexchange.com/images/stories/320x50mobile_standard.gif</string>

</value>

</data>

</array>

</value>

</member>

- <member>

<name>large</name>

- <value>

- <array>

- <data>

- <value>

<string>http://4.bp.blogspot.com/_5pkoVwxuN90/S-RpGWWgshI/AAAAAAAACoU/HlB9I5WOo4g/s400/iphone_jd1003.jpg</string>

</value>

- <value>

<string>http://3.bp.blogspot.com/_5pkoVwxuN90/S_vW6bHZgMI/AAAAAAAACqs/CpQ05gcYtTA/s320/iphone-ps5032.jpg</string>

</value>

</data>

</array>

</value>

</member>

- <member>

<name>medium</name>

- <value>

- <array>

- <data>

- <value>

<string>http://upload.wikimedia.org/wikipedia/en/b/be/Ads-300x250.gif</string>

</value>

- <value>

<string>http://www.wrestling-online.com/images/ads/300x250/nomercy2008.jpg</string>

</value>

</data>

</array>

</value>

</member>

</struct>

</value>

</member>

网友答案:

Even though I'm rather big fan of pull parser, this time I would recommend using SAX parser instead. It causes some overhead compared to pull parser but your DefaultHandler implementation could be rather simple as it's possible to listen to only very few elements you're interested in, and their contents.

With pull parser the problem is not so much to find start tags - but finding related end tag - while you should search for child tags/contents at the same time. I'm guessing all this would end up into such recursion, rather hard to handle and error prone if xml has some unexpected content, you find stack based parsing in SAX more comfortable quite quickly.

Also if your xml size is counted in megabytes, and you have possibility to have changes made on server, you might find using GZIPped stream more comfortable. If data comes over socket/http/etc that is.

网友答案:

You can try to use xPath:

http://developer.android.com/reference/javax/xml/xpath/package-summary.html

分享给朋友:
您可能感兴趣的文章:
随机阅读: