1 2 4 import java.io.FileReader ; 5 import java.io.IOException ; 6 import java.io.StringReader ; 7 8 import java.io.FileInputStream ; 9 import java.io.InputStreamReader ; 10 11 12 import org.xmlpull.v1.XmlPullParser; 13 import org.xmlpull.v1.XmlPullParserException; 14 import org.xmlpull.v1.XmlPullParserFactory; 15 16 21 public class MyXmlPullApp 22 { 23 24 public final static String SAMPLE_XML = 25 "<?xml version=\"1.0\"?>\n"+ 26 "\n"+ 27 "<poem xmlns=\"http://www.megginson.com/ns/exp/poetry\">\n"+ 28 "<title>Roses are Red</title>\n"+ 29 "<l>Roses are red,</l>\n"+ 30 "<l>Violets are blue;</l>\n"+ 31 "<l>Sugar is sweet,</l>\n"+ 32 "<l>And I love you.</l>\n"+ 33 "</poem>"; 34 35 public static void main (String args[]) 36 throws XmlPullParserException, IOException 37 { 38 XmlPullParserFactory factory = XmlPullParserFactory.newInstance( 39 System.getProperty(XmlPullParserFactory.PROPERTY_NAME), null); 40 factory.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); 42 43 XmlPullParser xpp = factory.newPullParser(); 44 System.out.println("parser implementation class is "+xpp.getClass()); 45 46 MyXmlPullApp app = new MyXmlPullApp(); 47 48 if(args.length == 0) { 49 System.out.println("Parsing simple sample XML"); xpp.setInput( new StringReader ( SAMPLE_XML ) ); 51 app.processDocument(xpp); 52 } else { 53 for (int i = 0; i < args.length; i++) { 54 System.out.println("Parsing file: "+args[i]); 55 xpp.setInput ( new FileReader ( args [i] ) ); 56 app.processDocument(xpp); 59 } 60 } 61 } 62 63 64 public void processDocument(XmlPullParser xpp) 65 throws XmlPullParserException, IOException 66 { 67 int eventType = xpp.getEventType(); 68 do { 69 if(eventType == xpp.START_DOCUMENT) { 70 System.out.println("Start document"); 71 } else if(eventType == xpp.END_DOCUMENT) { 72 System.out.println("End document"); 73 } else if(eventType == xpp.START_TAG) { 74 processStartElement(xpp); 75 } else if(eventType == xpp.END_TAG) { 76 processEndElement(xpp); 77 } else if(eventType == xpp.TEXT) { 78 processText(xpp); 79 } 80 eventType = xpp.next(); 81 } while (eventType != xpp.END_DOCUMENT); 82 } 83 84 85 public void processStartElement (XmlPullParser xpp) 86 { 87 String name = xpp.getName(); 88 String uri = xpp.getNamespace(); 89 if ("".equals (uri)) { 90 System.out.println("Start element: " + name); 91 } else { 92 System.out.println("Start element: {" + uri + "}" + name); 93 } 94 } 95 96 97 public void processEndElement (XmlPullParser xpp) 98 { 99 String name = xpp.getName(); 100 String uri = xpp.getNamespace(); 101 if ("".equals (uri)) 102 System.out.println("End element: " + name); 103 else 104 System.out.println("End element: {" + uri + "}" + name); 105 } 106 107 int holderForStartAndLength[] = new int[2]; 108 109 public void processText (XmlPullParser xpp) throws XmlPullParserException 110 { 111 char ch[] = xpp.getTextCharacters(holderForStartAndLength); 112 int start = holderForStartAndLength[0]; 113 int length = holderForStartAndLength[1]; 114 System.out.print("Characters: \""); 115 for (int i = start; i < start + length; i++) { 116 switch (ch[i]) { 117 case '\\': 118 System.out.print("\\\\"); 119 break; 120 case '"': 121 System.out.print("\\\""); 122 break; 123 case '\n': 124 System.out.print("\\n"); 125 break; 126 case '\r': 127 System.out.print("\\r"); 128 break; 129 case '\t': 130 System.out.print("\\t"); 131 break; 132 default: 133 System.out.print(ch[i]); 134 break; 135 } 136 } 137 System.out.print("\"\n"); 138 } 139 } 140 141 | Popular Tags |