XPP3-一个非常快的XML解析器

前端之家收集整理的这篇文章主要介绍了XPP3-一个非常快的XML解析器前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
1、XPP3 也叫MXP1

XPP —— XML Pull Parser,XPP3是一个非常快的XML解析器,是第三版的XPP。

XPP2是XPP的第二版。
XPP3是XPP2的继承者,实现了http://www.xmlpull.org定义的XmlPull API 。
XPP3只能用于解析XML,不支持节点树的增加操作。
看到有人做的对比测试,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/

使用举例:http://www.xmlpull.org/v1/download/unpacked/doc/quick_intro.html


MyXmlPull.java

============

[java] view plain copy
  1. importjava.io.FileReader;
  2. importjava.io.IOException;
  3. importjava.io.StringReader;
  4. importorg.xmlpull.v1.XmlPullParser;
  5. importorg.xmlpull.v1.XmlPullParserException;
  6. importorg.xmlpull.v1.XmlPullParserFactory;
  7. /**
  8. *AnexampleofanapplicationthatusesXMLPULLV1API.
  9. *
  10. *@author<ahref="http://www.extreme.indiana.edu/~aslom/">AleksanderSlominski</a>
  11. */
  12. publicclassMyXmlPullApp
  13. {
  14. publicfinalstaticStringSAMPLE_XML=
  15. "<?xmlversion=\"1.0\"?>\n"+
  16. "\n"+
  17. "<poemxmlns=\"http://www.megginson.com/ns/exp/poetry\">\n"+
  18. "<title>RosesareRed</title>\n"+
  19. "<l>Rosesarered,</l>\n"+
  20. "<l>Violetsareblue;</l>\n"+
  21. "<l>Sugarissweet,108); border-left-width:3px"> "<l>AndIloveyou.</l>\n"+
  22. "</poem>";
  23. publicstaticvoidmain(Stringargs[])
  24. throwsXmlPullParserException,IOException
  25. {
  26. XmlPullParserFactoryfactory=XmlPullParserFactory.newInstance();
  27. factory.setNamespaceAware(true);
  28. XmlPullParserxpp=factory.newPullParser();
  29. System.out.println("parserimplementationclassis"+xpp.getClass());
  30. MyXmlPullAppapp=newMyXmlPullApp();
  31. if(args.length==0){
  32. System.out.println("ParsingsimplesampleXML");//:\n"+SAMPLE_XML);
  33. xpp.setInput(newStringReader(SAMPLE_XML));
  34. app.processDocument(xpp);
  35. }else{
  36. for(inti=0;i<args.length;i++){
  37. System.out.println("Parsingfile:"+args[i]);
  38. xpp.setInput(newFileReader(args[i]));
  39. app.processDocument(xpp);
  40. }
  41. }
  42. publicvoidprocessDocument(XmlPullParserxpp)
  43. inteventType=xpp.getEventType();
  44. do{
  45. if(eventType==xpp.START_DOCUMENT){
  46. System.out.println("Startdocument");
  47. }elseif(eventType==xpp.END_DOCUMENT){
  48. System.out.println("Enddocument");
  49. }elseif(eventType==xpp.START_TAG){
  50. processStartElement(xpp);
  51. }elseif(eventType==xpp.END_TAG){
  52. processEndElement(xpp);
  53. }elseif(eventType==xpp.TEXT){
  54. processText(xpp);
  55. eventType=xpp.next();
  56. }while(eventType!=xpp.END_DOCUMENT);
  57. publicvoidprocessStartElement(XmlPullParserxpp)
  58. Stringname=xpp.getName();
  59. Stringuri=xpp.getNamespace();
  60. if("".equals(uri)){
  61. System.out.println("Startelement:"+name);
  62. }else{
  63. System.out.println("Startelement:{"+uri+"}"+name);
  64. publicvoidprocessEndElement(XmlPullParserxpp)
  65. if("".equals(uri))
  66. System.out.println("Endelement:"+name);
  67. else
  68. System.out.println("Endelement:{"+uri+"}"+name);
  69. intholderForStartAndLength[]=newint[2];
  70. publicvoidprocessText(XmlPullParserxpp)throwsXmlPullParserException
  71. charch[]=xpp.getTextCharacters(holderForStartAndLength);
  72. intstart=holderForStartAndLength[0];
  73. intlength=holderForStartAndLength[1];
  74. System.out.print("Characters:\"");
  75. for(inti=start;i<start+length;i++){
  76. switch(ch[i]){
  77. case'\\':
  78. System.out.print("\\\\");
  79. break;
  80. case'"':
  81. System.out.print("\\\"");
  82. break;
  83. case'\n':
  84. System.out.print("\\n");
  85. case'\r':
  86. System.out.print("\\r");
  87. case'\t':
  88. System.out.print("\\t");
  89. default:
  90. System.out.print(ch[i]);
  91. System.out.print("\"\n");
  92. }
原文链接:https://www.f2er.com/xml/294759.html

猜你在找的XML相关文章