XPP —— XML Pull Parser,XPP3是一个非常快的XML解析器,是第三版的XPP。
XPP2是XPP的第二版。
XPP3是XPP2的继承者,实现了http://www.xmlpull.org定义的XmlPull API 。
看到有人做的对比测试,XPP3 比DOM4J 快6倍,也就是使用了1/6时间。
XPP3使用的是Apache 1.1 variant License 协议。
http://www.extreme.indiana.edu/xgws/xsoap/xpp/mxp1/index.html
http://mvnrepository.com/artifact/xpp3/xpp3
使用举例:http://www.xmlpull.org/v1/download/unpacked/doc/quick_intro.html
MyXmlPull.java
============
- importjava.io.FileReader;
- importjava.io.IOException;
- importjava.io.StringReader;
- importorg.xmlpull.v1.XmlPullParser;
- importorg.xmlpull.v1.XmlPullParserException;
- importorg.xmlpull.v1.XmlPullParserFactory;
- /**
- *AnexampleofanapplicationthatusesXMLPULLV1API.
- *
- *@author<ahref="http://www.extreme.indiana.edu/~aslom/">AleksanderSlominski</a>
- */
- publicclassMyXmlPullApp
- {
- publicfinalstaticStringSAMPLE_XML=
- "<?xmlversion=\"1.0\"?>\n"+
- "\n"+
- "<poemxmlns=\"http://www.megginson.com/ns/exp/poetry\">\n"+
- "<title>RosesareRed</title>\n"+
- "<l>Rosesarered,</l>\n"+
- "<l>Violetsareblue;</l>\n"+
- "<l>Sugarissweet,108); border-left-width:3px"> "<l>AndIloveyou.</l>\n"+
- "</poem>";
- publicstaticvoidmain(Stringargs[])
- throwsXmlPullParserException,IOException
- {
- XmlPullParserFactoryfactory=XmlPullParserFactory.newInstance();
- factory.setNamespaceAware(true);
- XmlPullParserxpp=factory.newPullParser();
- System.out.println("parserimplementationclassis"+xpp.getClass());
- MyXmlPullAppapp=newMyXmlPullApp();
- if(args.length==0){
- System.out.println("ParsingsimplesampleXML");//:\n"+SAMPLE_XML);
- xpp.setInput(newStringReader(SAMPLE_XML));
- app.processDocument(xpp);
- }else{
- for(inti=0;i<args.length;i++){
- System.out.println("Parsingfile:"+args[i]);
- xpp.setInput(newFileReader(args[i]));
- app.processDocument(xpp);
- }
- }
- publicvoidprocessDocument(XmlPullParserxpp)
- inteventType=xpp.getEventType();
- do{
- if(eventType==xpp.START_DOCUMENT){
- System.out.println("Startdocument");
- }elseif(eventType==xpp.END_DOCUMENT){
- System.out.println("Enddocument");
- }elseif(eventType==xpp.START_TAG){
- processStartElement(xpp);
- }elseif(eventType==xpp.END_TAG){
- processEndElement(xpp);
- }elseif(eventType==xpp.TEXT){
- processText(xpp);
- eventType=xpp.next();
- }while(eventType!=xpp.END_DOCUMENT);
- publicvoidprocessStartElement(XmlPullParserxpp)
- Stringname=xpp.getName();
- Stringuri=xpp.getNamespace();
- if("".equals(uri)){
- System.out.println("Startelement:"+name);
- }else{
- System.out.println("Startelement:{"+uri+"}"+name);
- publicvoidprocessEndElement(XmlPullParserxpp)
- if("".equals(uri))
- System.out.println("Endelement:"+name);
- else
- System.out.println("Endelement:{"+uri+"}"+name);
- intholderForStartAndLength[]=newint[2];
- publicvoidprocessText(XmlPullParserxpp)throwsXmlPullParserException
- charch[]=xpp.getTextCharacters(holderForStartAndLength);
- intstart=holderForStartAndLength[0];
- intlength=holderForStartAndLength[1];
- System.out.print("Characters:\"");
- for(inti=start;i<start+length;i++){
- switch(ch[i]){
- case'\\':
- System.out.print("\\\\");
- break;
- case'"':
- System.out.print("\\\"");
- break;
- case'\n':
- System.out.print("\\n");
- case'\r':
- System.out.print("\\r");
- case'\t':
- System.out.print("\\t");
- default:
- System.out.print(ch[i]);
- System.out.print("\"\n");
- }