Example usage for javax.xml.stream XMLStreamReader hasNext

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

Introduction

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

Prototype

public boolean hasNext() throws XMLStreamException;

Source Link

Document

Returns true if there are more parsing events and false if there are no more events.

Usage

From source file:Main.java

public static void consumeEnd(XMLStreamReader xmlRdr, String elementName) throws XMLStreamException {
    while (xmlRdr.hasNext()) {
        if (xmlRdr.isEndElement() && xmlRdr.getLocalName().equals(elementName)) {
            xmlRdr.next();/* w w w. ja  v  a 2  s.  c  om*/
            return;
        }
        xmlRdr.next();
    }
    throw new IllegalStateException(
            "expected start tag <" + elementName + ">, found '" + xmlRdr.getText() + "'");
}

From source file:Main.java

/**
 * Moves the parser to the next element.
 *//* ww w.  j  a va2s  .  co  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

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]";
                }//w w w . java  2 s  .  c o 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);
        }
    }
}

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/*from  ww  w .  ja  va2 s  . co  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/*from   www  . j av  a2  s.  c om*/
 * @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

public static void checkWellformed(String out) throws Exception {

    synchronized (wellformedSet) {
        if (wellformedSet.contains(out)) {
            return;
        }/*from  w ww  . j  a v  a  2  s . c om*/
    }

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

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

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

From source file:XmlReaderToWriter.java

public static void writeAll(XMLStreamReader xmlr, XMLStreamWriter writer) throws XMLStreamException {
    while (xmlr.hasNext()) {
        write(xmlr, writer);//from ww  w .  ja  v  a  2  s .c om
        xmlr.next();
    }
    write(xmlr, writer); // write the last element
    writer.flush();
}

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//  www .j a  v a  2 s  . c om
 * @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

/**
 * Borrowed from org.apache.xml.security.test.stax.utils.XmlReaderToWriter
 *///w  ww. ja  v  a2 s . co m
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;//from   w w w  .j av a 2s  .  com
    }

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