XMLPULL V1 API is ...
Java version of XMLPULL V1 API provides:
XMLPULL is API and it requires implementation to run. See list of implementations on XMLPULL website. After downloading one of implementations of XMLPULL API V1 (version 1.0.*) add jar file to your CLASSPATH. As XMLPULL uses factory there is no need to explicitly state what is class with parser implementation: it will be picked up automatically from implementation jar file.
First we need to create an instance of parser. To do this three steps are required:
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
Next step is to set parser input:
xpp.setInput ( new FileReader ( args [i] ) );and now we can start parsing!
Typical XMLPULL applicaition will repeatedly call next() function to retrieve next event, process event until the event is END_DOCUMENT:
public void processDocument(XmlPullParser xpp)
throws XmlPullParserException, IOException
{
int eventType = xpp.getType();
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);
}
Let see how to process start tag. Processing end tag is very similar - main difference is that the end tag has no attributes.
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);
}
}
And now let see how element content is retrieved and printed:
public void processText (XmlPullParser xpp) throws XmlPullParserException
{
char ch[] = xpp.getTextCharacters();
int start = xpp.getTextCharactersStart();
int length = xpp.getTextCharactersLength();
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");
}
java MyXmlPullApp
parser implementation class is class org.xmlpull.xpp3.PullParser
Parsing simple sample XML
Start document
Start element: {http://www.megginson.com/ns/exp/poetry}poem
Characters: "\n"
Start element: {http://www.megginson.com/ns/exp/poetry}title
Characters: "Roses are Red"
End element: {http://www.megginson.com/ns/exp/poetry}title
Characters: "\n"
Start element: {http://www.megginson.com/ns/exp/poetry}l
Characters: "Roses are red,"
End element: {http://www.megginson.com/ns/exp/poetry}l
Characters: "\n"
Start element: {http://www.megginson.com/ns/exp/poetry}l
Characters: "Violets are blue;"
End element: {http://www.megginson.com/ns/exp/poetry}l
Characters: "\n"
Start element: {http://www.megginson.com/ns/exp/poetry}l
Characters: "Sugar is sweet,"
End element: {http://www.megginson.com/ns/exp/poetry}l
Characters: "\n"
Start element: {http://www.megginson.com/ns/exp/poetry}l
Characters: "And I love you."
End element: {http://www.megginson.com/ns/exp/poetry}l
Characters: "\n"
End element: {http://www.megginson.com/ns/exp/poetry}poem