Example usage for javax.xml.stream XMLStreamReader next

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

Introduction

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

Prototype

public int next() throws XMLStreamException;

Source Link

Document

Get next parsing event - a processor may return all contiguous character data in a single chunk, or it may split it into several chunks.

Usage

From source file:Main.java

public static String nextString(XMLStreamReader reader) throws XMLStreamException {
    int type;//  ww w.j  a v  a 2 s.  c  o  m
    StringBuilder result = new StringBuilder();
    while ((type = reader.next()) != XMLStreamReader.END_ELEMENT) {
        switch (type) {
        case XMLStreamReader.START_ELEMENT:
            throw new IllegalStateException("Found start element parsing text.");

        case XMLStreamReader.CDATA:
        case XMLStreamReader.SPACE:
        case XMLStreamReader.CHARACTERS:
            result.append(reader.getText());
        }
    }
    return result.toString();
}

From source file:Main.java

/**
 * Gets the version of the XML./*from  w  w  w  .  ja  v a 2s  .c  o m*/
 *
 * @param path the XML file.
 * @return the corresponding version of the XML.
 */
public static String getXMLVersion(final Path path) {
    try (InputStream inputStream = Files.newInputStream(path)) {
        XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(inputStream);
        for (int event; (event = reader.next()) != XMLStreamConstants.END_DOCUMENT;) {
            if (event == XMLStreamConstants.START_ELEMENT) {
                String tmp = reader.getLocalName();
                if ("persistence".equals(tmp)) {
                    for (int i = 0; i < reader.getAttributeCount(); i++) {
                        if ("version".equals(reader.getAttributeName(i).toString())) {
                            return reader.getAttributeValue(i);
                        }
                    }
                }
            }
        }
    } catch (Exception ex) {
        throw new RuntimeException("Error reading the persistence.xml version.", ex);
    }
    return null;
}

From source file:Main.java

private static void positionXMLStreamReaderAtAnyElement(XMLStreamReader xsr) throws Exception {
    while (xsr.hasNext()) {
        if (xsr.getEventType() == XMLStreamReader.START_ELEMENT && "ANY-ELEMENT".equals(xsr.getLocalName())) {
            break;
        }//from ww w  .j a  va2 s.  c o  m
        xsr.next();
    }
}

From source file:Main.java

/**
 * Advance the reader to the next end element and return true. Return false if next end element is not found
 * @param reader a XMLStreamReader//  w  ww .  j  a va  2 s .c  o m
 * @return True when successfully advanced the reader.
 * @throws XMLStreamException Exception when reading from the XMLStreamReader fails.
 */
public static boolean advanceToNextEndElement(XMLStreamReader reader) throws XMLStreamException {
    while (reader.getEventType() != XMLStreamConstants.END_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

/**
 * Advance the reader to the next start element and return true. Return false if next end element is not found
 * @param reader a XMLStreamReader/*w w w.j  a va 2 s . 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

/**
 * Borrowed from org.apache.xml.security.test.stax.utils.XmlReaderToWriter
 *//* w  w  w.j  a  v a2  s.c  om*/
public static void writeAll(XMLStreamReader xmlr, XMLStreamWriter writer) throws XMLStreamException {
    while (xmlr.hasNext()) {
        xmlr.next();
        write(xmlr, writer);
    }
    writer.flush();
}

From source file:Main.java

public static void skipToEndElement(XMLStreamReader in) throws XMLStreamException {
    if (!in.isStartElement()) {
        return;/*  www .jav a2 s .c  om*/
    }

    int tagDepth = 1;
    while (tagDepth > 0 && in.hasNext()) {
        in.next();
        if (in.isStartElement()) {
            tagDepth++;
        } else if (in.isEndElement()) {
            tagDepth--;
        }
    }
}

From source file:Main.java

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

From source file:Main.java

/**
 * Skip new lines after the end elements.
 * This function should be called when the cursor is pointing to CHARACTER and leaves the cursor
 * at END_ELEMENT OR START_ELEMENT/*  ww w  .jav  a2s  . c  o m*/
 * @param xmlStreamReader
 */
public static void skipNewLines(XMLStreamReader xmlStreamReader) throws XMLStreamException {
    if (xmlStreamReader.isCharacters()) {
        while (xmlStreamReader.isCharacters() && xmlStreamReader.hasNext())
            xmlStreamReader.next();
    }
}

From source file:Main.java

/**
 * Skips an element's complete content. This method assumes that the
 * <code>START_ELEMENT</code> has already be passed, and when it terminates,
 * the stream will be positioned at the <code>END_ELEMENT</code>.
 * //w  w w .j  a v a  2 s  . c o  m
 * @param reader The stream reader to read.
 * @throws XMLStreamException If an error occurs reading the stream.
 */
public static final void skipElementContent(XMLStreamReader reader) throws XMLStreamException {

    int depth = 0;
    while (depth >= 0) {

        reader.next();
        if (reader.isStartElement()) {

            depth++;

        } else if (reader.isEndElement()) {

            depth--;

        }

    }

}