Here you can find the source of moveReaderToTag(XMLStreamReader reader, QName... tags)
Parameter | Description |
---|---|
tags | array of tags |
reader | XMLStreamReader which should be moved |
Parameter | Description |
---|---|
XMLStreamException | See XMLStreamReader |
public static boolean moveReaderToTag(XMLStreamReader reader, QName... tags) throws XMLStreamException
//package com.java2s; // License: GPL. For details, see LICENSE file. import javax.xml.namespace.QName; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamReader; public class Main { /**//from w ww. ja v a 2 s .c o m * Moves reader to first occurrence of the structure equivalent of Xpath tags[0]/tags[1]../tags[n]. If fails to find * moves the reader to the closing tag of current tag * * @param tags array of tags * @param reader XMLStreamReader which should be moved * @return true if tag was found, false otherwise * @throws XMLStreamException See {@link XMLStreamReader} */ public static boolean moveReaderToTag(XMLStreamReader reader, QName... tags) throws XMLStreamException { QName stopTag = reader.getName(); int currentLevel = 0; QName searchTag = tags[currentLevel]; QName parentTag = null; QName skipTag = null; for (int event = 0; //skip current element, so we will not skip it as a whole reader.hasNext() && !(event == XMLStreamReader.END_ELEMENT && stopTag.equals(reader.getName())); event = reader.next()) { if (event == XMLStreamReader.END_ELEMENT && skipTag != null && skipTag.equals(reader.getName())) { skipTag = null; } if (skipTag == null) { if (event == XMLStreamReader.START_ELEMENT) { if (searchTag.equals(reader.getName())) { currentLevel += 1; if (currentLevel >= tags.length) { return true; // found! } parentTag = searchTag; searchTag = tags[currentLevel]; } else { skipTag = reader.getName(); } } if (event == XMLStreamReader.END_ELEMENT && parentTag != null && parentTag.equals(reader.getName())) { currentLevel -= 1; searchTag = parentTag; if (currentLevel >= 0) { parentTag = tags[currentLevel]; } else { parentTag = null; } } } } return false; } }