前端之家收集整理的这篇文章主要介绍了
xml 使用PULL解析器,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
import java.io.InputStream;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlSerializer;
import android.util.Xml;
/*
<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<message from="192.168.10.110" to="192.168.10.112"
direct="0" time="2014-04-12 15:20:10" xml:lang="en">
<data length="1000">json....data</data>
<thread>thread_id</thread>
</message>
*/
private void xml_pull_test()
{
//xml-pull 生成:
XmlSerializer serializer = Xml.newSerializer(); //由android.util.Xml创建一个XmlSerializer实例
StringWriter writer = new StringWriter();
try {
serializer.setOutput(writer); //设置输出方向为writer
serializer.startDocument("UTF-8",true);
serializer.startTag("","message");
serializer.attribute("","from","192.168.10.110");
serializer.attribute("","to","192.168.10.112");
serializer.attribute("","direct","0"); // direct=0 server->client,1=client->server,2=client-client,serializer.attribute("","time","2014-04-12 15:20:10");
serializer.attribute("","app","xserver");//app
serializer.attribute("","xml:lang","en");
serializer.startTag("","data");
serializer.attribute("","length","1000");
serializer.text("json....data");
serializer.endTag("","data");
serializer.startTag("","thread");
serializer.text("thread_id");
serializer.endTag("","thread");
serializer.endTag("","message");
serializer.endDocument();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
System.out.println("[result] Error1:"+e.getMessage());
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
System.out.println("[result] Error2:"+e.getMessage());
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("[result] Error3:"+e.getMessage());
}
System.out.println(writer.toString());
//xml-pull 解析:
String xmlString=writer.toString();
XmlPullParser parser = Xml.newPullParser();
ByteArrayInputStream is = new ByteArrayInputStream(xmlString.getBytes());//.getBytes("UTF-8")
try {
parser.setInput(is,"UTF-8"); //设置输入流 并指明编码方式
int eventType = parser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
switch (eventType) {
case XmlPullParser.START_DOCUMENT:
break;
case XmlPullParser.START_TAG:
String tag=parser.getName();
System.out.println("<"+tag+">");
if (tag.equalsIgnoreCase("message")) {
int na=parser.getAttributeCount();
System.out.println("na="+na);
for (int i=0;i<na;i++){
String name=parser.getAttributeName(i);
String value=parser.getAttributeValue(i);
String type=parser.getAttributeType(i);
System.out.println("["+i+"]:"+name+","+value+","+type);
}
//Integer.parseInt(parser.getAttributeValue(ns,"id")));
} else if (tag.equalsIgnoreCase("data")) {
eventType = parser.next();
String str=parser.getText();
System.out.println(str);
} else if (tag.equalsIgnoreCase("thread")) {
eventType = parser.next();
String str=parser.getText();//Integer.parseInt(parser.getText())
System.out.println(str);
}
break;
case XmlPullParser.END_TAG:
String tagE=parser.getName();
System.out.println("</"+tagE+">");
if (tagE.equalsIgnoreCase("message")) {
}
break;
}
eventType = parser.next();
}
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
System.out.println("[result] Error1:"+e.getMessage());
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
System.out.println("[result] Error2:"+e.getMessage());
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("[result] Error3:"+e.getMessage());
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
原文链接:https://www.f2er.com/xml/298876.html