XmlPull v1实例解析

XmlPull是一种轻量级的XML处理工具,很类似SAX。
特点: xpp3-1.1.3.4-RC6.jar不占空间(相对而言),101K
SAX方式处理XML,不占内存空间(相对而言)
因此在J2ME上颇有用武之地。
以下是一个实例:

/**
* Date: 2007-5-27
* 测试XmlPull
* @author Mou
* @version 1.0
*/
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import java.io.*;

class MyXmlPullApp {
XmlPullParserFactory factory;
XmlPullParser xpp;
String sPath = "";

/*
get instance of XmlPull factory
(optional step) by default factory will produce parsers that are not namespace aware;
to change setNamespaceAware() function must be called
create an instance of the parser
*/
public MyXmlPullApp(String sPath){
this.sPath = sPath;
try{
XmlPullParserFactory factory = XmlPullParserFactory.newInstance(System.getProperty(XmlPullParserFactory.PROPERTY_NAME), null);
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();

xpp.setInput (new FileReader(sPath));
this.processDocument(xpp);
}catch(XmlPullParserException ex){
ex.printStackTrace();
}catch(FileNotFoundException ex){
ex.printStackTrace();
}catch(IOException ex){
ex.printStackTrace();
}
}

/*
Typical XmlPull applicaition will repeatedly call next() function to retrieve next event,
process event until the even is END_DOCUMENT:
非常类似于SAX的处理
*/
public void processDocument(XmlPullParser xpp)
throws XmlPullParserException, IOException{
int eventType = xpp.getEventType();
do {
if(eventType == xpp.START_DOCUMENT) {
System.out.println("Start document");
} else if(eventType == xpp.END_DOCUMENT) {
System.out.println("End document");
} else if(eventType == xpp.START_TAG) {
processStartElement(xpp);
} else if(eventType == xpp.END_TAG) {
processEndElement(xpp);
} else if(eventType == xpp.TEXT) {
processText(xpp);
}
eventType = xpp.next();
} while (eventType != xpp.END_DOCUMENT);
}

/*
Processing end tag is very similar - main difference is that the end tag has no attributes.
*/
public void processStartElement (XmlPullParser xpp){
String name = xpp.getName();
String uri = xpp.getNamespace();
if ("".equals (uri)) {
System.out.println("Start element: " + name);
} else {
System.out.println("Start element: {" + uri + "}" + name);
}
}
public void processEndElement (XmlPullParser xpp){
String name = xpp.getName();
String uri = xpp.getNamespace();
if ("".equals (uri)) {
System.out.println("End element: " + name);
} else {
System.out.println("End element: {" + uri + "}" + name);
}
}

/*
how element content is retrieved and printed:
*/
int holderForStartAndLength[] = new int[2];
public void processText (XmlPullParser xpp) throws XmlPullParserException{
char ch[] = xpp.getTextCharacters(holderForStartAndLength);
int start = holderForStartAndLength[0];
int length = holderForStartAndLength[1];
System.out.print("Characters: "");
for (int i = start; i < start + length; i++) {
switch (ch[i]) {
case '':
System.out.print("");
break;
case '"':
System.out.print(""");
break;
case ' ':
System.out.print(" ");
break;
case ' ':
System.out.print(" ");
break;
case ' ':
System.out.print(" ");
break;
default:
System.out.print(ch[i]);
break;
}
}
System.out.print("" ");
}

public static void main(String[] args){
new MyXmlPullApp("web.xml");
}
}

[@more@]

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/11049438/viewspace-967477/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/11049438/viewspace-967477/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值