Java tutorial
//package com.java2s; /* * For license please see accompanying LICENSE.txt file (available also at http://www.xmlpull.org/). * According to www.xmlpull.org, this code is in the public domain. */ import java.io.IOException; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; public class Main { /** * directly jumps forward to start tag, ignoring any structure */ public static void jump(final XmlPullParser pp, final String tagName) throws XmlPullParserException, IOException { if (!jumpToStartTag(pp, null, tagName)) throw new IllegalStateException("cannot find <" + tagName + " />"); } /** * This method bypasses all events until it finds a start tag that has passed in namespace (if not null) and * namespace (if not null). * * @return true if such START_TAG was found or false otherwise (and parser is on END_DOCUMENT). */ public static boolean jumpToStartTag(final XmlPullParser pp, final String tagNamespace, final String tagName) throws XmlPullParserException, IOException { if (tagNamespace == null && tagName == null) throw new IllegalArgumentException( "namespace and name argument can not be both null:" + pp.getPositionDescription()); while (true) { final int eventType = pp.next(); if (eventType == XmlPullParser.START_TAG) { final String name = pp.getName(); final String namespace = pp.getNamespace(); boolean matches = (tagNamespace != null && tagNamespace.equals(namespace)) || (tagName != null && tagName.equals(name)); if (matches) return true; } else if (eventType == XmlPullParser.END_DOCUMENT) { return false; } } } public static void next(final XmlPullParser pp) throws XmlPullParserException, IOException { skipSubTree(pp); pp.next(); } /** * Skip sub tree that is currently porser positioned on. <br> * NOTE: parser must be on START_TAG and when funtion returns parser will be positioned on corresponding END_TAG */ public static void skipSubTree(final XmlPullParser pp) throws XmlPullParserException, IOException { pp.require(XmlPullParser.START_TAG, null, null); int level = 1; while (level > 0) { final int eventType = pp.next(); if (eventType == XmlPullParser.END_TAG) --level; else if (eventType == XmlPullParser.START_TAG) ++level; } } }