Android Pull解析Xml字串
阿新 • • 發佈:2019-02-08
1、要解析的xml格式如下:
- <UpdateResultNeedUpdate="true">
- <FileUrlvalue="http://download.note.com/res/6650fd66-551f-47b5-bcd4-0971c391f896"/>
- <CurrentVersionvalue="0.0.0.1"/>
- <WebSitevalue="http://note.do.com/"/>
- </UpdateResult>
在解析之前,將該xml賦值給String變數xmlStr
2、解析UpdateResult的屬性NeedUpdate,並把結果賦值給isNeedUpdate,解析FileUrl的value屬性,並把結果賦值給FileUrl:
- XmlPullParser parser = Xml.newPullParser();
- parser.setInput(new StringReader(xmlStr));
- int event = parser.getEventType();
- while (event != XmlPullParser.END_DOCUMENT) {
- switch (event) {
- case XmlPullParser.START_DOCUMENT:
- break;
- case XmlPullParser.START_TAG:
- if
("UpdateResult".equals(parser.getName())) {- int count = parser.getAttributeCount();
- for (int i = 0; i < count; i++) {
- String key = parser.getAttributeName(i);
- if ("NeedUpdate".equals(key)) {
- isNeedUpdate= "true"
.equals(parser.getAttributeValue(i));- }
- }
- } elseif ("FileUrl".equals(parser.getName())) {
- int count = parser.getAttributeCount();
- for (int i = 0; i < count; i++) {
- String key = parser.getAttributeName(i);
- if ("value".equals(key)) {
- FileUrl = parser.getAttributeValue(i);
- }
- }
- }
- break;
- case XmlPullParser.END_TAG:
- break;
- }
- event = parser.next();
- }
主要用到類:org.xmlpull.v1.XmlPullParser和java.io.StringReader
針對實際應用到的一個xml字串,進行解析,得到item的值。
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?> <!DOCTYPE boost_serialization> <boost_serialization signature="serialization::archive" version="9"> <field class_id="0" tracking_level="0" version="0"> <count>1</count> <item_version>0</item_version> <item class_id="1" tracking_level="0" version="0"> <first>streams</first> <second>0</second> </item> </field> <RowCount>1</RowCount> <rowdata class_id="2" tracking_level="0" version="0"> <count>1</count> <item_version>0</item_version> <item>{"res":{"Stream1":{"ip":"172.16.128.67","name":"20140909_ch0_h","app":"teach_app","vol":1,"type":1,"alias":"\u9ed8\u8ba4\u522b\u540d"},"Stream2":{"ip":"172.16.128.67","name":"20140909_ch1_h","app":"teach_app","vol":0,"type":1,"alias":"\u9ed8\u8ba4\u522b\u540d"},"Stream3":{"ip":"172.16.128.67","name":"20140909_ch2_h","app":"teach_app","vol":0,"type":1,"alias":"\u9ed8\u8ba4\u522b\u540d"},"Stream4":{"ip":"172.16.128.67","name":"20140909_ch3_h","app":"teach_app","vol":0,"type":1,"alias":"\u9ed8\u8ba4\u522b\u540d"}},"mov":{"Stream1":{"ip":"172.16.128.67","name":"20140909_ch4_h","app":"teach_app","vol":0,"type":1,"alias":"\u9ed8\u8ba4\u522b\u540d"}}} </item> </rowdata> </boost_serialization>
package com.charein.util; import java.io.StringReader; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; import android.util.Log; import android.util.Xml; public class XmlParser { private static String TAG = "XmlParser"; private String xmlString; public boolean Parser(String xml) { XmlPullParser parser = Xml.newPullParser(); try { parser.setInput(new StringReader(xml)); } catch (XmlPullParserException e) { // TODO Auto-generated catch block e.printStackTrace(); Log.d(TAG, "XmlPullParserException: " + e); } try { int event = parser.getEventType(); while (event != XmlPullParser.END_DOCUMENT) { switch (event) { case XmlPullParser.START_DOCUMENT: break; case XmlPullParser.START_TAG: if ("item".equals(parser.getName())) { int count = parser.getAttributeCount(); if (count == 0) { xmlString = parser.nextText(); return true; } } break; case XmlPullParser.END_TAG: break; } event = parser.next(); } } catch (Exception e) { // TODO: handle exception Log.e(TAG, "Exception: " + e); return false; } return false; } public String getXmlString() { return xmlString; } }