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

/**
 * Test if the current event is an element tag with the given namespace and name.
 * If the namespace URI is null it is not checked for equality,
 * if the local name is null it is not checked for equality.
 * @param reader must not be {@code null}
 * @param event START_ELEMENT or END_ELEMENT
 * @param uri the namespace URI of the element, may be null
 * @param name the local name of the element, may be null
 * @return true if the required values are matched, false otherwise
 *///www  .j a  v  a 2  s  . co  m
private static boolean isElement(XMLStreamReader reader, int event, String uri, String name) {
    if (reader.getEventType() != event)
        return false;

    if (uri != null) {
        String found = reader.getNamespaceURI();
        if (!found.equals(uri))
            return false;
    }

    if (name != null) {
        String found = reader.getLocalName();
        if (!found.equals(name))
            return false;
    }

    return true;
}

From source file:StaxCursorTest.java

private static void printComment(XMLStreamReader xmlr) {
    if (xmlr.getEventType() == xmlr.COMMENT) {
        System.out.print("<!--" + xmlr.getText() + "-->");
    }//from w  w  w .  ja  v  a  2  s.  co m
}

From source file:StaxCursorTest.java

private static void printPIData(XMLStreamReader xmlr) {
    if (xmlr.getEventType() == XMLEvent.PROCESSING_INSTRUCTION) {
        System.out.print("<?" + xmlr.getPITarget() + " " + xmlr.getPIData() + "?>");
    }//  w ww .  j  av  a 2s.c  o  m
}

From source file:Main.java

/**
 * 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/*from   w w  w.j  av a2 s.c om*/
 * @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;
}

From source file:StaxCursorTest.java

private static void printStartDocument(XMLStreamReader xmlr) {
    if (xmlr.START_DOCUMENT == xmlr.getEventType()) {
        System.out.println("<?xml version=\"" + xmlr.getVersion() + "\"" + " encoding=\""
                + xmlr.getCharacterEncodingScheme() + "\"" + "?>");
    }//from  www .  ja va 2  s.co  m
}

From source file:Main.java

@SuppressWarnings("empty-statement")
public static Optional<String> nextTag(XMLStreamReader reader) throws XMLStreamException {
    while (reader.hasNext() && reader.next() != XMLStreamReader.START_ELEMENT)
        ;/*from  w w w .  j a  v a 2  s.  com*/
    if (reader.getEventType() == XMLStreamReader.END_DOCUMENT) {
        return Optional.empty();
    } else {
        return Optional.of(reader.getName().getLocalPart());
    }
}

From source file:Main.java

public static String parseValue(XMLStreamReader xmlRdr) throws XMLStreamException {
    // consume start tag
    xmlRdr.next();/*from   w ww . ja va  2s . com*/
    String val = null;

    if (xmlRdr.getEventType() == XMLStreamConstants.CHARACTERS) {
        val = xmlRdr.getText();
        xmlRdr.next();
    }
    skipToEndElement(xmlRdr);
    return val;
}

From source file:Main.java

public static void dumpXML(XMLStreamReader parser) throws XMLStreamException {
    int depth = 0;
    do {/* w  w  w .  j  a v a2s  .  c  o  m*/
        switch (parser.getEventType()) {
        case XMLStreamConstants.START_ELEMENT:
            for (int i = 1; i < depth; ++i) {
                System.out.print("    ");
            }
            System.out.print("<");
            System.out.print(parser.getLocalName());
            for (int i = 0; i < parser.getAttributeCount(); ++i) {
                System.out.print(" ");
                System.out.print(parser.getAttributeLocalName(i));
                System.out.print("=\"");
                System.out.print(parser.getAttributeValue(i));
                System.out.print("\"");
            }
            System.out.println(">");

            ++depth;
            break;
        case XMLStreamConstants.END_ELEMENT:
            --depth;
            for (int i = 1; i < depth; ++i) {
                System.out.print("    ");
            }
            System.out.print("</");
            System.out.print(parser.getLocalName());
            System.out.println(">");
            break;
        case XMLStreamConstants.CHARACTERS:
            for (int i = 1; i < depth; ++i) {
                System.out.print("    ");
            }

            System.out.println(parser.getText());
            break;
        }

        if (depth > 0)
            parser.next();
    } while (depth > 0);
}

From source file:Main.java

/**
 * Moves the parser to the next start element.
 * /*from   www .j  a v  a 2  s  . c  o m*/
 * @return {@code true} if another start element has been found,
 *         {@code false} otherwise
 */
public static boolean findNextStartElemenet(XMLStreamReader parser) throws XMLStreamException {
    assert parser != null;

    while (true) {
        int event = parser.getEventType();

        if (event == XMLStreamConstants.START_ELEMENT) {
            return true;
        }

        if (parser.hasNext()) {
            parser.next();
        } else {
            return false;
        }
    }
}

From source file:Main.java

public static String parseValue(XMLStreamReader xmlRdr, String elementName) throws XMLStreamException {
    String val = null;
    while (true) {
        xmlRdr.next();/*w  w w.  ja  v a  2s.  c  o m*/
        int event2 = xmlRdr.getEventType();
        if (event2 == XMLStreamConstants.CHARACTERS) {
            val = xmlRdr.getText();
        } else if (event2 == XMLStreamConstants.END_ELEMENT && xmlRdr.getLocalName().equals(elementName)) {
            return val;
        }
    }
}