1. 程式人生 > >Android Pull解析Xml字串

Android Pull解析Xml字串

1、要解析的xml格式如下:

  1. <UpdateResultNeedUpdate="true">
  2.     <FileUrlvalue="http://download.note.com/res/6650fd66-551f-47b5-bcd4-0971c391f896"/>
  3.     <CurrentVersionvalue="0.0.0.1"/>
  4.     <WebSitevalue="http://note.do.com/"/>
  5. </UpdateResult>

在解析之前,將該xml賦值給String變數xmlStr

2、解析UpdateResult的屬性NeedUpdate,並把結果賦值給isNeedUpdate,解析FileUrl的value屬性,並把結果賦值給FileUrl:

  1. XmlPullParser parser = Xml.newPullParser();  
  2. parser.setInput(new StringReader(xmlStr));  
  3. int event = parser.getEventType();  
  4. while (event != XmlPullParser.END_DOCUMENT) {  
  5. switch (event) {  
  6.     case XmlPullParser.START_DOCUMENT:  
  7.         break;  
  8.     case XmlPullParser.START_TAG:  
  9.         if
     ("UpdateResult".equals(parser.getName())) {  
  10.             int count = parser.getAttributeCount();  
  11.             for (int i = 0; i < count; i++) {  
  12.                 String key = parser.getAttributeName(i);  
  13.                 if ("NeedUpdate".equals(key)) {  
  14.                     isNeedUpdate= "true"
    .equals(parser.getAttributeValue(i));  
  15.                 }  
  16.             }  
  17.         } elseif ("FileUrl".equals(parser.getName())) {  
  18.                 int count = parser.getAttributeCount();  
  19.                 for (int i = 0; i < count; i++) {  
  20.                     String key = parser.getAttributeName(i);  
  21.                     if ("value".equals(key)) {  
  22.                         FileUrl = parser.getAttributeValue(i);  
  23.                     }  
  24.                 }  
  25.             }  
  26.         break;  
  27.     case XmlPullParser.END_TAG:  
  28.         break;  
  29.     }  
  30.     event = parser.next();  
  31. }  

主要用到類: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;
	}
}