XmlPull v1例項解析

mouqj發表於2007-09-01
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/,如需轉載,請註明出處,否則將追究法律責任。

相關文章