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

static protected void skip(XMLStreamReader parser) throws XMLStreamException {
    while (parser.hasNext()) {
        int eventType = parser.next();
        if (eventType == XMLStreamConstants.START_ELEMENT) {
            skip(parser);//  w  w w.  ja va  2s  .c o m
        } else if (eventType == XMLStreamConstants.END_ELEMENT) {
            return;
        }
    }
}

From source file:Main.java

public static boolean skipToElement(XMLStreamReader in) throws XMLStreamException {
    for (int code = in.getEventType(); code != XMLStreamReader.END_DOCUMENT; code = in.next()) {
        if (code == XMLStreamReader.START_ELEMENT || code == XMLStreamReader.END_ELEMENT) {
            return true;
        }/* w w  w. j  av  a2s .  c om*/
    }
    return false;
}

From source file:Main.java

public static void checkWellformed(String out) throws Exception {

    synchronized (wellformedSet) {
        if (wellformedSet.contains(out)) {
            return;
        }/*from   w  w w  .j  a  va2  s .c o  m*/
    }

    XMLStreamReader reader = inputFactory.createXMLStreamReader(new StringReader(out));

    do {
        reader.next();
    } while (reader.hasNext());

    synchronized (wellformedSet) {
        wellformedSet.add(out);
    }
}

From source file:Main.java

public static boolean goToNextXBeforeY(XMLStreamReader reader, String X, String Y) throws XMLStreamException {
    while (reader.hasNext()) {
        int code = reader.next();
        if (code == XMLStreamReader.END_ELEMENT && reader.getLocalName().equals(Y)) {
            return false;
        }/*ww w .  j av a2 s.  c  o  m*/
        if (code == XMLStreamReader.START_ELEMENT && reader.getLocalName().equals(X)) {
            return true;
        }
    }
    return false;
}

From source file:Main.java

public static void skipElement(XMLStreamReader parser) throws XMLStreamException {
    int numEndTags = 1;
    while (numEndTags > 0) {
        switch (parser.next()) {
        case XMLStreamConstants.START_ELEMENT:
            ++numEndTags;//go down
            break;
        case XMLStreamConstants.END_ELEMENT:
            --numEndTags;//go up
            break;
        }// www.j a  va 2s.c  om
    }
}

From source file:Main.java

/**
 * Moves the parser to the next element.
 *///from   ww w  .  j av a  2 s.  c  o m
public static boolean next(XMLStreamReader parser) throws XMLStreamException {
    assert parser != null;

    if (parser.hasNext()) {
        parser.next();
        return true;
    }

    return false;
}

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;
        }/*from   w  w w.  j  a  v a  2  s.c  o m*/
    }
    return false;
}

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;
        }//w w  w .ja  v a2s  . c om
    }
    return false;
}

From source file:Main.java

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

From source file:Main.java

private static void parseRestOfDocument(XMLStreamReader reader) throws XMLStreamException {

    while (reader.hasNext()) {
        int type = reader.next();
        switch (type) {
        case XMLStreamConstants.START_ELEMENT:
            System.out.println(reader.getLocalName());
            if (reader.getNamespaceURI() != null) {
                String prefix = reader.getPrefix();
                if (prefix == null) {
                    prefix = "[None]";
                }/*from w  w  w.  j av  a  2  s . co  m*/
                System.out.println("prefix = '" + prefix + "', URI = '" + reader.getNamespaceURI() + "'");
            }

            if (reader.getAttributeCount() > 0) {
                for (int i = 0; i < reader.getAttributeCount(); i++) {
                    System.out.println("Attribute (name = '" + reader.getAttributeLocalName(i) + "', value = '"
                            + reader.getAttributeValue(i) + "')");
                    String attURI = reader.getAttributeNamespace(i);
                    if (attURI != null) {
                        String attPrefix = reader.getAttributePrefix(i);
                        if (attPrefix == null || attPrefix.equals("")) {
                            attPrefix = "[None]";
                        }
                        System.out.println("prefix=" + attPrefix + ",URI=" + attURI);
                    }
                }
            }

            break;
        case XMLStreamConstants.END_ELEMENT:
            System.out.println("XMLStreamConstants.END_ELEMENT");
            break;
        case XMLStreamConstants.CHARACTERS:
            if (!reader.isWhiteSpace()) {
                System.out.println("CD:" + reader.getText());
            }
            break;
        case XMLStreamConstants.DTD:
            System.out.println("DTD:" + reader.getText());
            break;
        case XMLStreamConstants.SPACE:
            System.out.println(" ");
            break;
        case XMLStreamConstants.COMMENT:
            System.out.println(reader.getText());
            break;
        default:
            System.out.println(type);
        }
    }
}