xml
XML parsing with XML Pull
With this tutorial we are going to show you how to perform XML parsing using XML Pull in Android.
There are two popular methods in XML parsing: SAX and DOM. But a new method that gains more and more attention is the “pull parsing” method. The Android SDK includes support for XML Pull parsing via the XML Pull package
The basic steps to perform pull Parsing in Android are:
- Create a new instance of the
XmlPullParserFactory
class - Create a new
XmlPullParser
using thenewPullParser
factory method. - Obtain an
InputStream
from a URL connection (because your XML is an internet resource) - Use the several Event Type Tags and methods to parse your document appropriately and obtain the elements and the corresponding values of the XML Document
package com.javacodegeeks.android.xml.pull; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlPullParserFactory; import android.app.Activity; import android.os.Bundle; import android.util.Log; import com.javacodegeeks.android.xml.pull.model.Image; import com.javacodegeeks.android.xml.pull.model.Movie; public class XmlPullParserActivity extends Activity { private static final String xmlUrl = "http://dl.dropbox.com/u/7215751/JavaCodeGeeks/AndroidFullAppTutorialPart03/Transformers+2007.xml"; private final String TAG = getClass().getSimpleName(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); try { List<Movie> movies = parseFromUrl(); for (Movie movie : movies) { Log.d(TAG, "Movie:"+movie); } } catch (Exception e) { Log.e(TAG, "Error while parsing", e); } } private List<Movie> parseFromUrl() throws XmlPullParserException, IOException { List<Movie> moviesList = null; XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); factory.setNamespaceAware(false); XmlPullParser parser = factory.newPullParser(); URL url = new URL(xmlUrl); URLConnection ucon = url.openConnection(); InputStream is = ucon.getInputStream(); parser.setInput(is, null); parser.nextTag(); parser.nextTag(); parser.nextTag(); parser.nextTag(); parser.nextText(); parser.nextTag(); moviesList = parseMovies(parser); return moviesList; } private List<Movie> parseMovies(XmlPullParser parser) throws XmlPullParserException, IOException { List<Movie> moviesList = new LinkedList<Movie>(); Log.d(TAG, "parseMovies tag " + parser.getName()); while (parser.nextTag() == XmlPullParser.START_TAG) { Log.d(TAG, "parsing movie"); Movie movie = parseMovie(parser); moviesList.add(movie); } return moviesList; } private Movie parseMovie(XmlPullParser parser) throws XmlPullParserException, IOException { Movie movie = new Movie(); Log.d(TAG, "parseMovie tag " + parser.getName()); while (parser.nextTag() == XmlPullParser.START_TAG) { if (parser.getName().equals("name")) { movie.name = parser.nextText(); } else if (parser.getName().equals("score")) { movie.score = parser.nextText(); } else if (parser.getName().equals("images")) { Image image = parseImage(parser); movie.imagesList = new ArrayList<Image>(); movie.imagesList.add(image); } else if (parser.getName().equals("version")) { movie.version = parser.nextText(); } else { parser.nextText(); } } return movie; } private Image parseImage(XmlPullParser parser) throws XmlPullParserException, IOException { Image image = new Image(); Log.d(TAG, "parseImage tag " + parser.getName()); while (parser.nextTag() == XmlPullParser.START_TAG) { if (parser.getName().equals("image")) { image.type = parser.getAttributeValue(null, "type"); image.url = parser.getAttributeValue(null, "url"); image.size = parser.getAttributeValue(null, "size"); image.width = Integer.parseInt(parser.getAttributeValue(null, "width")); image.height = Integer.parseInt(parser.getAttributeValue(null, "height")); } parser.next(); } return image; } }
package com.javacodegeeks.android.xml.pull.model; import java.util.ArrayList; public class Movie { public String score; public String popularity; public boolean translated; public boolean adult; public String language; public String originalName; public String name; public String type; public String id; public String imdbId; public String url; public String votes; public String rating; public String certification; public String overview; public String released; public String version; public String lastModifiedAt; public ArrayList<Image> imagesList; }
package com.javacodegeeks.android.xml.pull.model; public class Image { public String type; public String url; public String size; public int width; public int height; }
This was an example of how to perform XML Pull Parsing in Android.
Related Article: