Snippet: XML-Pull-Parser with XPP3

December 28th, 2012 by

When there is XML to be parsed sometimes we’re chosing a DOM parser, sometimes a SAX parser and sometimes we’re using an XML pull parser, especially on Android.

The following short snippet shows how to use the xpp3 xml pull parse to fetch some information in xml format from a public issue tracker and to extract issue details.


 

Adding Dependencies

Just one dependency needed here: xpp3

SBT

Simply use the following build.sbt

name := "xml-pull-parser"
 
version := "0.0.1"
 
organization := "com.hascode.tutorial"
 
libraryDependencies += "xpp3" % "xpp3" % "1.1.4c"

Maven

If you’re in Maven, the following pom.xml should match your needs

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
         http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.hascode.tutorial</groupId>
<artifactId>xml-pull-parser</artifactId>
<version>0.0.1</version>
<url>http://www.hascode.com</url>
<dependencies>
    <dependency>
        <groupId>xpp3</groupId>
        <artifactId>xpp3</artifactId>
        <version>1.1.4c</version>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>
    </plugins>
</build>
</project>

Fetching, Parsing, Output..

We’re using Atlassian’s public Jira/Issue tracker at https://jira.atlassian.com/ to fetch some xml and we’re extracting title and link for each issue …

package com.hascode.tutorial;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
 
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
 
public class ParserExample {
	private static final String urlString = "https://jira.atlassian.com/sr/jira.issueviews:searchrequest-xml/temp/SearchRequest.xml?jqlQuery=&tempMax=20";
	private static final String ITEM_TITLE = "title";
	private static final String ITEM_LINK = "link";
 
	public static void main(final String[] args) throws XmlPullParserException,
			MalformedURLException, IOException {
		XmlPullParser xpp = XmlPullParserFactory.newInstance().newPullParser();
		URL url = new URL(urlString);
		BufferedReader in = new BufferedReader(new InputStreamReader(
				url.openStream()));
		try {
			xpp.setInput(in);
			while (xpp.getEventType() != XmlPullParser.END_DOCUMENT) {
				xpp.next();
				if (ITEM_TITLE.equals(xpp.getName())) {
					System.out.print("Issue: " + xpp.nextText());
				}
				if (ITEM_LINK.equals(xpp.getName())) {
					System.out.print(" at " + xpp.nextText() + "\n");
				}
			}
		} finally {
			in.close();
		}
	}
}

Running the code should produce the following output..

$ sbt run
[info] Loading global plugins from /somepath/.sbt/plugins
[info] Set current project to xml-pull-parser (in build file:/data/project/xmlpullparser-example/)
[info] Running com.hascode.tutorial.ParserExample
Issue: [WBS-32] Filter Results do not scroll when exceeding visible screen height at https://jira.atlassian.com/browse/WBS-32
Issue: [WBS-29] Tarea de prueba at https://jira.atlassian.com/browse/WBS-29
Issue: [WBS-28] asdsa at https://jira.atlassian.com/browse/WBS-28
Issue: [WBS-27] Twitter Feed Broken at https://jira.atlassian.com/browse/WBS-27
Issue: [WBS-26] Stacktrace shown in logs after view the wallboard at https://jira.atlassian.com/browse/WBS-26
Issue: [WBS-25] No Wallboard gadget for Rapid board burndown at https://jira.atlassian.com/browse/WBS-25
Issue: [WBS-24] Installing GreenHopper into JIRA after Wallboards causes wallboard to go down until the next restart. at https://jira.atlassian.com/browse/WBS-24
Issue: [WBS-23] Warnings from Wallboard plugin when starting up Jira at https://jira.atlassian.com/browse/WBS-23
Issue: [WBS-22] Wallboards broken against JIRA 5.1 EAP at https://jira.atlassian.com/browse/WBS-22
Issue: [WBS-19] Wallboars fails to start jira 5.0.2 at https://jira.atlassian.com/browse/WBS-19
Issue: [WBS-18] another wallboard showing in background at https://jira.atlassian.com/browse/WBS-18
[..]
[success] Total time: 6 s, completed Dec 28, 2012 8:57:37 AM

Tutorial Sources

Please feel free to take a look at the sources on my Bitbucket repository or if you’ve got git installed just check it out by using:

git clone https://bitbucket.org/hascode/xml-pull-parser-example.git

Resources

Tags: , , , , , ,

Leave a Reply

Please leave these two fields as-is:

Protected by Invisible Defender. Showed 403 to 81,013 bad guys.

Search
Tags
Categories