| 注册
请输入搜索内容

热门搜索

Java Linux MySQL PHP JavaScript Hibernate jQuery Nginx
fmms
13年前发布

使用 XmlPull 解析 XML 的简单例子

     XmlPull和Sax类似,是基于流(stream)操作文件,然后根据节点事件回调开发者编写的处理程序。因为是基于流的处理,因此Xmlpull和 Sax都比较节约内存资源,不会象Dom那样要把所有节点以对橡树的形式展现在内存中。 但Xmlpull比Sax更简明,而且不需要扫描完整个流。    <br /> 而    <a href="http://www.open-open.com/lib/view/open1326465868030.html" rel="nofollow" target="_blank">MXP1</a>是实现XmlPull的一个java类库    <pre class="brush:java; toolbar: true; auto-links: false;">import java.io.FileReader; import java.io.IOException; import java.io.StringReader;  import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlPullParserFactory;  /**  * An example of an application that uses XMLPULL V1 API.  *  * @author <a href="http://www.extreme.indiana.edu/~aslom/">Aleksander Slominski</a>  */ public class MyXmlPullApp {      public final static String SAMPLE_XML =         "<?xml version=\"1.0\"?>\n"+         "\n"+         "<poem xmlns=\"http://www.megginson.com/ns/exp/poetry\">\n"+         "<title>Roses are Red</title>\n"+         "<l>Roses are red,</l>\n"+         "<l>Violets are blue;</l>\n"+         "<l>Sugar is sweet,</l>\n"+         "<l>And I love you.</l>\n"+         "</poem>";      public static void main (String args[])         throws XmlPullParserException, IOException     {         XmlPullParserFactory factory = XmlPullParserFactory.newInstance();         factory.setNamespaceAware(true);         XmlPullParser xpp = factory.newPullParser();         System.out.println("parser implementation class is "+xpp.getClass());          MyXmlPullApp app = new MyXmlPullApp();          if(args.length == 0) {             System.out.println("Parsing simple sample XML");//:\n"+ SAMPLE_XML);             xpp.setInput( new StringReader( SAMPLE_XML ) );             app.processDocument(xpp);         } else {             for (int i = 0; i < args.length; i++) {                 System.out.println("Parsing file: "+args[i]);                 xpp.setInput ( new FileReader ( args [i] ) );                 app.processDocument(xpp);             }         }     }       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);     }       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);     }      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 '\n':                     System.out.print("\\n");                     break;                 case '\r':                     System.out.print("\\r");                     break;                 case '\t':                     System.out.print("\\t");                     break;                 default:                     System.out.print(ch[i]);                     break;             }         }         System.out.print("\"\n");     } }</pre>     
 本文由用户 fmms 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
 转载本站原创文章,请注明出处,并保留原始链接、图片水印。
 本站是一个以用户分享为主的开源技术平台,欢迎各类分享!
 本文地址:https://www.open-open.com/lib/view/open1326516289905.html
XmlPull XML操作类库