Java tutorial
//package com.java2s; /******************************************************************************* * Copyright (c) 2006-2010 eBay Inc. All Rights Reserved. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 *******************************************************************************/ import javax.xml.stream.XMLStreamConstants; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamReader; public class Main { /** * Advance the reader to the next element after start element and return true. * Return false if next start element is not found * @param reader a XMLStreamReader * @param startElement Name of the start element. * @return True when successfully advanced the reader. * @throws XMLStreamException Exception when reading from the XMLStreamReader fails. */ public static boolean advanceToAfterStartElement(XMLStreamReader reader, String startElement) throws XMLStreamException { while (!(reader.getEventType() == XMLStreamConstants.START_ELEMENT && reader.hasName() && reader.getLocalName().equalsIgnoreCase(startElement))) { //String name = (reader.hasName()? reader.getLocalName() : ""); if (reader.hasNext()) { reader.next(); } else { // reach the end of elements in reader. Not found. return false; } } // found the startElement. Consume that start element also if (reader.hasNext()) { reader.next(); return true; } return false; } }