Commits

Micha Kops  committed 07add5a

Initial import.

  • Participants

Comments (0)

Files changed (4)

+.project
+.settings
+target
+bin
+infinitest.filters
+.classpath
+# XML Pull Parser Example
+
+A simple example how to parse some XML from the public Atlassian JIRA instance using the XML Pull Parser API.
+
+Please feel free to take a look at [my blog] for some more snippets.
+
+---
+
+**2012 Micha Kops / hasCode.com**
+<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>

File src/main/java/com/hascode/tutorial/ParserExample.java

+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=200";
+	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();
+		}
+	}
+}