Example usage for javax.xml.stream XMLStreamReader getEventType

List of usage examples for javax.xml.stream XMLStreamReader getEventType

Introduction

In this page you can find the example usage for javax.xml.stream XMLStreamReader getEventType.

Prototype

public int getEventType();

Source Link

Document

Returns an integer code that indicates the type of the event the cursor is pointing to.

Usage

From source file:Main.java

/**
 * Advance the reader to the next start element and return true. Return false if next end element is not found
 * @param reader a XMLStreamReader/*from  www  . ja v  a  2s.co  m*/
 * @return True when successfully advanced the reader.
 * @throws XMLStreamException Exception when reading from the XMLStreamReader fails.
 */
public static boolean advanceToNextStartElement(XMLStreamReader reader) throws XMLStreamException {
    while (reader.getEventType() != XMLStreamConstants.START_ELEMENT) {
        if (reader.hasNext()) {
            reader.next();
        } else {
            // reach the end of elements in reader. Not found.
            return false;
        }
    }
    return true;
}

From source file:Main.java

/**
 * Skip current element, including all its content.
 * Precondition: the current event is START_ELEMENT.
 * Postcondition: the current event is the corresponding END_ELEMENT.
 * Similar to {@link XMLStreamReader#nextTag()}, but also skips text content.
 * @param reader must not be {@code null}
 * @throws XMLStreamException if the current event is not START_ELEMENT or there is an error processing the underlying XML source
 *//*from w  w w.j a  v a  2 s.  c  om*/
public static void skipElement(XMLStreamReader reader) throws XMLStreamException {
    if (reader.getEventType() != START_ELEMENT)
        throw new XMLStreamException("expected start of element", reader.getLocation());

    int depth = 0;
    while (reader.hasNext()) {
        int event = reader.next();
        if (event == START_ELEMENT) {
            ++depth;
        } else if (event == END_ELEMENT) {
            --depth;
            if (depth == -1)
                break;
        }
    }
}

From source file:Main.java

public static boolean skipToEndOfElement(XMLStreamReader in, String localName) throws XMLStreamException {
    for (int code = in.getEventType(); code != XMLStreamReader.END_DOCUMENT; code = in.next()) {
        if (code == XMLStreamReader.END_ELEMENT && (localName == null || localName.equals(in.getLocalName()))) {
            return true;
        }// w ww . j a  v  a2s  .  c  o  m
    }
    return false;
}

From source file:Main.java

/**
 * Test if reader is on a start tag named tagLocalName.
 *
 * @param reader       xml stream reader
 * @param tagLocalName tag local name// w w  w .ja va  2 s  .  c o  m
 * @return true if reader is on a start tag named tagLocalName
 */
public static boolean isStartTag(XMLStreamReader reader, String tagLocalName) {
    return (reader.getEventType() == XMLStreamConstants.START_ELEMENT)
            && (reader.getLocalName().equals(tagLocalName));
}

From source file:Main.java

public static boolean skipToStartOfElement(XMLStreamReader in, String localName) throws XMLStreamException {
    for (int code = in.getEventType(); code != XMLStreamReader.END_DOCUMENT; code = in.next()) {
        if (code == XMLStreamReader.START_ELEMENT
                && (localName == null || localName.equals(in.getLocalName()))) {
            return true;
        }//from ww w.j a  v  a  2s  .c  o  m
    }
    return false;
}

From source file:Main.java

public static boolean isElement(XMLStreamReader xmlRdr, int eventType, String tagName) {
    int event = xmlRdr.getEventType();
    if (event == eventType) {
        String locName = xmlRdr.getLocalName();
        if (locName.equals(tagName))
            return true;
    }//from w  w  w  .  j  ava2 s.  c om
    return false;
}

From source file:Main.java

public static String getStateName(XMLStreamReader reader) {
    return getStateName(reader.getEventType());
}

From source file:Main.java

public static void skipToEndElement(XMLStreamReader xmlRdr) throws XMLStreamException {
    do {//from w w  w  .  j a va 2 s . com
        if (xmlRdr.getEventType() == XMLStreamConstants.END_ELEMENT) {
            break;
        }
        xmlRdr.next();
    } while (xmlRdr.hasNext());
}

From source file:Main.java

private static void printPIData(XMLStreamReader xmlr) {
    if (xmlr.getEventType() == XMLEvent.PROCESSING_INSTRUCTION) {
        System.out.println(" PI target = " + xmlr.getPITarget());
        System.out.println(" PI Data = " + xmlr.getPIData());
    }/*from  w w w.  j  a va 2  s  .  com*/
}

From source file:Main.java

public static void skipElements(XMLStreamReader reader, Integer... elements) throws XMLStreamException {
    int eventType = reader.getEventType();
    List<Integer> types = Arrays.asList(elements);
    while (types.contains(eventType)) {
        eventType = reader.next();/*from  ww w .  j ava2  s.  com*/
    }
}