KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > XmlPullCount


1 /* -*- c-basic-offset: 4; indent-tabs-mode: nil; -*- //------100-columns-wide------>|*/
2 // for license please see accompanying LICENSE.txt file (available also at http://www.xmlpull.org/)
3

4 import java.io.File JavaDoc;
5 import java.io.FileReader JavaDoc;
6 import java.io.IOException JavaDoc;
7 import java.io.StringReader JavaDoc;
8
9 import org.xmlpull.v1.XmlPullParser;
10 import org.xmlpull.v1.XmlPullParserException;
11 import org.xmlpull.v1.XmlPullParserFactory;
12
13 /**
14  * Simple example that counts XML elements, characters and attributes.
15  *
16  * @author <a HREF="http://www.extreme.indiana.edu/~aslom/">Aleksander Slominski</a>
17  */

18 public class XmlPullCount
19 {
20     public final static String JavaDoc SAMPLE_XML =
21         "<?xml version=\"1.0\"?>\n"+
22         "\n"+
23         "<poem xmlns=\"http://www.megginson.com/ns/exp/poetry\">\n"+
24         "<title>Roses are Red</title>\n"+
25         "<l>Roses are red,</l>\n"+
26         "<l>Violets are blue;</l>\n"+
27         "<l>Sugar is sweet,</l>\n"+
28         "<l>And I love you.</l>\n"+
29         "</poem>";
30     int countChars;
31     int countAttribs;
32     int countSTags;
33
34     public static void main (String JavaDoc args[])
35         throws XmlPullParserException, IOException JavaDoc
36     {
37
38
39         XmlPullParserFactory factory = XmlPullParserFactory.newInstance(
40             System.getProperty(XmlPullParserFactory.PROPERTY_NAME), null);
41         factory.setNamespaceAware(true);
42         System.out.println("using factory "+factory.getClass());
43
44         XmlPullParser xpp = factory.newPullParser();
45         System.out.println("using parser "+xpp.getClass());
46
47         XmlPullCount app = new XmlPullCount();
48
49         for(int c = 0; c < 2; ++c) {
50             System.out.println("run#"+c);
51             app.resetCounters();
52             if(args.length == 0) {
53                 System.out.println("Parsing simple sample XML length="+SAMPLE_XML.length());
54                 xpp.setInput( new StringReader JavaDoc( SAMPLE_XML ) );
55                 app.countXml(xpp);
56             } else {
57                 //r (int i = 0; i < args.length; i++) {
58

59                 File JavaDoc f = new File JavaDoc(args[0]);
60                 System.out.println("Parsing file: "+args[0]+" length="+f.length());
61                 xpp.setInput ( new FileReader JavaDoc ( args [0] ) );
62                 app.countXml(xpp);
63                 //
64
}
65             app.printReport();
66         }
67         System.out.println("finished");
68     }
69
70     public void resetCounters() {
71         countChars = countSTags = countAttribs = 0;
72     }
73
74     public void printReport() {
75         System.out.println("characters="+countChars
76                                +" elements="+countSTags
77                                +" attributes="+countAttribs);
78
79     }
80
81     public void countXml(XmlPullParser xpp) throws XmlPullParserException, IOException JavaDoc {
82
83         int holderForStartAndLength[] = new int[2];
84
85
86         int eventType = xpp.getEventType();
87         if(eventType == xpp.START_DOCUMENT) {
88             eventType = xpp.next();
89         }
90         while (eventType != xpp.END_DOCUMENT) {
91             //System.out.println("pos="+xpp.getPositionDescription());
92
if(eventType == xpp.START_TAG) {
93                 ++countSTags;
94                 countAttribs += xpp.getAttributeCount();
95             } else if(eventType == xpp.TEXT) {
96                 //char ch[] = xpp.getTextCharacters(holderForStartAndLength);
97
xpp.getTextCharacters(holderForStartAndLength);
98                 //int start = holderForStartAndLength[0];
99
int length = holderForStartAndLength[1];
100                 countChars += length;
101             }
102             eventType = xpp.next();
103         }
104     }
105 }
106
Popular Tags