ITPub博客

首页 > 应用开发 > IT综合 > XmlPull v1实例解析

XmlPull v1实例解析

原创 IT综合 作者:mouqj 时间:2007-09-01 14:53:39 0 删除 编辑
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/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
  • 博文量
    33
  • 访问量
    116244