Java XML QName moveReaderToTag(XMLStreamReader reader, QName... tags)

Here you can find the source of moveReaderToTag(XMLStreamReader reader, QName... tags)

Description

Moves reader to first occurrence of the structure equivalent of Xpath tags[0]/tags[1]../tags[n].

License

GNU General Public License

Parameter

Parameter Description
tags array of tags
reader XMLStreamReader which should be moved

Exception

Parameter Description
XMLStreamException See XMLStreamReader

Return

true if tag was found, false otherwise

Declaration

public static boolean moveReaderToTag(XMLStreamReader reader, QName... tags) throws XMLStreamException 

Method Source Code

//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;
    }
}

Related

  1. match(XMLStreamReader reader, QName name)
  2. matches(Node node, String requiredLocalName, QName requiredNamespace)
  3. matches(QName qname, Node node)
  4. matchingNamespace(Node node, QName requiredNamespace)
  5. matchNode(Node node, QName name)
  6. namedNode(QName qname)
  7. nodeToQName(Node node)
  8. parseXml(final Node node, final Map properties)
  9. printPath(List path)