android系統中采用PULL技術解析XML文檔比用SAX技術性能要好得多。建議android的開發者采用PULL技術解析XML文檔。跟SAX類似,只不過PULL技術返回的事件類型是整型,所以我們只要對返回的事件進行處理就行了。
Java代碼:
- package com.cn.service;
-
- import Java.io.IOException;
- import Java.io.InputStream;
- import Java.io.OutputStream;
- import Java.util.ArrayList;
- import Java.util.List;
-
- import org.xmlpull.v1.XMLPullParser;
- import org.xmlpull.v1.XMLSerializer;
-
- import android.util.XML;
-
- import com.cn.domain.Persion;
- /****
- * 采用Pull解析XML
- *
- * ****/
- public class PULLPersionService {
-
- public static List<Persion> getPersino(InputStream is)throws Exception{
- List<Persion> list = null;
- Persion persion = null;
- XmlPullParser parser = XML.newPullParser();
- parser.setInput(is, "UTF-8");
- int event = parser.getEventType();
- while(event != XMLPullParser.END_DOCUMENT){
- switch (event){
- case XMLPullParser.START_DOCUMENT:
- list = new ArrayList<Persion>();
- break;
- case XMLPullParser.START_TAG:
- if("person".equals(parser.getName())){//得到指針所指向的節點名稱
- int id = Integer.parseInt(parser.getAttributeValue(0));
- persion = new Persion();
- persion.setId(id);
- }
- if(persion != null){
- if("name".equals(parser.getName())){
- String name = parser.nextText();
- persion.setName(name);
- }else if("age".equals(parser.getName())){
- String age = parser.nextText();
- persion.setName(age);
- }
- }
- break;
- case XMLPullParser.END_TAG:
- if("person".equals(parser.getName())){
- list.add(persion);
- persion = null;
- }
- break;
- default:
- break;
- }
- }
- event = parser.next();
- return list;
- }
-
- public static void save(List<Persion> list,OutputStream os) throws IllegalArgumentException, IllegalStateException, IOException{
- XmlSerializer serializer = XML.newSerializer();
- serializer.setOutput(os, "UTF-8");
- serializer.startDocument("UTF-8", true);
- serializer.startTag(null, "persons");
- for(Persion persion : list){
- serializer.startTag(null, "person");
- serializer.attribute(null, "id", String.valueOf(persion.getId()));
- serializer.startTag(null, "name");
- serializer.text(persion.getName());
- serializer.endTag(null, "name");
-
- serializer.startTag(null, "age");
- serializer.text(String.valueOf(persion.getAge()));
- serializer.endTag(null, "age");
- serializer.endTag(null, "person");
- }
- serializer.endTag(null, "persons");
- serializer.endDocument();
- os.flush();
- os.close();
- }
- }
復制打印
- package com.cn.service;
-
- import Java.io.IOException;
- import Java.io.InputStream;
- import Java.io.OutputStream;
- import Java.util.ArrayList;
- import Java.util.List;
-
- import org.xmlpull.v1.XMLPullParser;
- import org.xmlpull.v1.XMLSerializer;
-
- import android.util.XML;
-
- import com.cn.domain.Persion;
- /****
- * 采用Pull解析XML
- *
- * ****/
- public class PULLPersionService {
-
- public static List<Persion> getPersino(InputStream is)throws Exception{
- List<Persion> list = null;
- Persion persion = null;
- XmlPullParser parser = XML.newPullParser();
- parser.setInput(is, "UTF-8");
- int event = parser.getEventType();
- while(event != XMLPullParser.END_DOCUMENT){
- switch (event){
- case XMLPullParser.START_DOCUMENT:
- list = new ArrayList<Persion>();
- break;
- case XMLPullParser.START_TAG:
- if("person".equals(parser.getName())){//得到指針所指向的節點名稱
- int id = Integer.parseInt(parser.getAttributeValue(0));
- persion = new Persion();
- persion.setId(id);
- }
- if(persion != null){
- if("name".equals(parser.getName())){
- String name = parser.nextText();
- persion.setName(name);
- }else if("age".equals(parser.getName())){
- String age = parser.nextText();
- persion.setName(age);
- }
- }
- break;
- case XMLPullParser.END_TAG:
- if("person".equals(parser.getName())){
- list.add(persion);
- persion = null;
- }
- break;
- default:
- break;
- }
- }
- event = parser.next();
- return list;
- }
-
- public static void save(List<Persion> list,OutputStream os) throws IllegalArgumentException, IllegalStateException, IOException{
- XmlSerializer serializer = XML.newSerializer();
- serializer.setOutput(os, "UTF-8");
- serializer.startDocument("UTF-8", true);
- serializer.startTag(null, "persons");
- for(Persion persion : list){
- serializer.startTag(null, "person");
- serializer.attribute(null, "id", String.valueOf(persion.getId()));
- serializer.startTag(null, "name");
- serializer.text(persion.getName());
- serializer.endTag(null, "name");
-
- serializer.startTag(null, "age");
- serializer.text(String.valueOf(persion.getAge()));
- serializer.endTag(null, "age");
- serializer.endTag(null, "person");
- }
- serializer.endTag(null, "persons");
- serializer.endDocument();
- os.flush();
- os.close();
- }
- }
package com.cn.service;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import Java.util.List;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlSerializer;
import android.util.Xml;
import com.cn.domain.Persion;
/****
* 采用Pull解析XML
*
* ****/
public class PULLPersionService {
public static List<Persion> getPersino(InputStream is)throws Exception{
List<Persion> list = null;
Persion persion = null;
XmlPullParser parser = Xml.newPullParser();
parser.setInput(is, "UTF-8");
int event = parser.getEventType();
while(event != XmlPullParser.END_DOCUMENT){
switch (event){
case XmlPullParser.START_DOCUMENT:
list = new ArrayList<Persion>();
break;
case XmlPullParser.START_TAG:
if("person".equals(parser.getName())){//得到指針所指向的節點名稱
int id = Integer.parseInt(parser.getAttributeValue(0));
persion = new Persion();
persion.setId(id);
}
if(persion != null){
if("name".equals(parser.getName())){
String name = parser.nextText();
persion.setName(name);
}else if("age".equals(parser.getName())){
String age = parser.nextText();
persion.setName(age);
}
}
break;
case XmlPullParser.END_TAG:
if("person".equals(parser.getName())){
list.add(persion);
persion = null;
}
break;
default:
break;
}
}
event = parser.next();
return list;
}
public static void save(List<Persion> list,OutputStream os) throws IllegalArgumentException, IllegalStateException, IOException{
XmlSerializer serializer = XML.newSerializer();
serializer.setOutput(os, "UTF-8");
serializer.startDocument("UTF-8", true);
serializer.startTag(null, "persons");
for(Persion persion : list){
serializer.startTag(null, "person");
serializer.attribute(null, "id", String.valueOf(persion.getId()));
serializer.startTag(null, "name");
serializer.text(persion.getName());
serializer.endTag(null, "name");
serializer.startTag(null, "age");
serializer.text(String.valueOf(persion.getAge()));
serializer.endTag(null, "age");
serializer.endTag(null, "person");
}
serializer.endTag(null, "persons");
serializer.endDocument();
os.flush();
os.close();
}
}