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 void main(String[] args) throws Exception {
    String filename = "yourXML.xml";

    XMLInputFactory xmlif = null;

    xmlif = XMLInputFactory.newInstance();
    xmlif.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.TRUE);
    xmlif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, Boolean.FALSE);
    xmlif.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.TRUE);
    xmlif.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);

    System.out.println("FACTORY: " + xmlif);
    System.out.println("filename = " + filename);

    FileInputStream fis = new FileInputStream(filename);

    XMLStreamReader xmlr = xmlif.createFilteredReader(xmlif.createXMLStreamReader(fis), new Main());

    int eventType = xmlr.getEventType();
    printEventType(eventType);/*w  w w.  j  av  a2s.  c om*/

    while (xmlr.hasNext()) {
        eventType = xmlr.next();
        printEventType(eventType);
        printName(xmlr, eventType);
        printText(xmlr);

        if (xmlr.isStartElement()) {
            printAttributes(xmlr);
        }
        printPIData(xmlr);
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String filename = "yourXML.xml";

    XMLInputFactory xmlif = null;

    xmlif = XMLInputFactory.newInstance();
    xmlif.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.TRUE);
    xmlif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, Boolean.FALSE);
    xmlif.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.TRUE);
    xmlif.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);

    System.out.println("FACTORY: " + xmlif);
    System.out.println("filename = " + filename);

    FileInputStream fis = new FileInputStream(filename);

    XMLStreamReader xmlr = xmlif.createFilteredReader(xmlif.createXMLStreamReader(fis), new MyFilter());

    int eventType = xmlr.getEventType();
    printEventType(eventType);/*from   w  w  w .ja v  a  2  s .c  o m*/

    while (xmlr.hasNext()) {
        eventType = xmlr.next();
        printEventType(eventType);
        printName(xmlr, eventType);
        printText(xmlr);

        if (xmlr.isStartElement()) {
            printAttributes(xmlr);
        }
        printPIData(xmlr);
    }
}

From source file:MyStreamFilter.java

public static void main(String[] args) throws Exception {
    String filename = "yourXML.xml";

    XMLInputFactory xmlif = null;

    xmlif = XMLInputFactory.newInstance();
    xmlif.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.TRUE);
    xmlif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, Boolean.FALSE);
    xmlif.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.TRUE);
    xmlif.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);

    System.out.println("FACTORY: " + xmlif);
    System.out.println("filename = " + filename);

    FileInputStream fis = new FileInputStream(filename);

    XMLStreamReader xmlr = xmlif.createFilteredReader(xmlif.createXMLStreamReader(fis), new MyStreamFilter());

    int eventType = xmlr.getEventType();
    printEventType(eventType);//from  w w w.j  av a 2  s  . co m

    while (xmlr.hasNext()) {
        eventType = xmlr.next();
        printEventType(eventType);
        printName(xmlr, eventType);
        printText(xmlr);

        if (xmlr.isStartElement()) {
            printAttributes(xmlr);
        }
        printPIData(xmlr);
    }
}

From source file:StaxCursorTest.java

public static void main(String[] args) throws Exception {

    String filename = "yourXML.xml";

    XMLInputFactory xmlif = null;

    xmlif = XMLInputFactory.newInstance();
    xmlif.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.TRUE);
    xmlif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, Boolean.FALSE);
    xmlif.setProperty(XMLInputFactory.IS_COALESCING, Boolean.FALSE);

    try {//  w ww.j  av  a 2 s .com
        XMLStreamReader xmlr = xmlif.createXMLStreamReader(filename, new FileInputStream(filename));
        int eventType = xmlr.getEventType();
        printStartDocument(xmlr);
        while (xmlr.hasNext()) {
            eventType = xmlr.next();
            printStartElement(xmlr);
            printEndElement(xmlr);
            printText(xmlr);
            printPIData(xmlr);
            printComment(xmlr);
        }
    } catch (XMLStreamException ex) {
        System.out.println(ex.getMessage());
        if (ex.getNestedException() != null) {
            ex.getNestedException().printStackTrace();
        }
    }
}

From source file:Main.java

/**
 * Load text of the current element./*from  ww w .  j  a  va 2 s.  c  om*/
 * @param reader XML reader.
 * @return Text of the element.
 * @throws XMLStreamException when the text cannot be read or XML is malformed.
 */
public static String loadElementText(XMLStreamReader reader) throws XMLStreamException {
    reader.next();
    if (reader.isEndElement()) {
        return "";
    }
    reader.require(XMLStreamConstants.CHARACTERS, null, null);
    String text = reader.getText();
    reader.next();
    reader.require(XMLStreamConstants.END_ELEMENT, null, null);
    return text;
}

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   www.j  ava  2  s  .  c  o m*/
    if (reader.getEventType() == XMLStreamReader.END_DOCUMENT) {
        return Optional.empty();
    } else {
        return Optional.of(reader.getName().getLocalPart());
    }
}

From source file:Main.java

public static int getNextEvent(XMLStreamReader xmlStreamReader) {
    Integer event = null;/* ww w . j  a  v a 2  s.c o m*/
    try {
        event = xmlStreamReader.next();
    } catch (XMLStreamException e) {
        e.printStackTrace();
    }
    return event;
}

From source file:Main.java

private static int countEvents(XMLStreamReader reader) throws XMLStreamException {
    int counter = 1;
    while (reader.hasNext()) {
        reader.next();
        counter++;//from  ww w.  j  ava 2 s .  c o  m
    }
    return counter;
}

From source file:Main.java

public static void ignoreTag(XMLStreamReader reader) throws XMLStreamException {
    int type;//from   w ww . j  a  v a  2  s  .c  om
    while ((type = reader.next()) != XMLStreamReader.END_ELEMENT) {
        switch (type) {
        case XMLStreamReader.START_ELEMENT:
            ignoreTag(reader);

        case XMLStreamReader.CDATA:
        case XMLStreamReader.SPACE:
        case XMLStreamReader.CHARACTERS:
        default:
        }
    }
}

From source file:Main.java

public static String parseValue(XMLStreamReader xmlRdr) throws XMLStreamException {
    // consume start tag
    xmlRdr.next();
    String val = null;

    if (xmlRdr.getEventType() == XMLStreamConstants.CHARACTERS) {
        val = xmlRdr.getText();
        xmlRdr.next();/*  w w  w .j a v  a  2 s. co  m*/
    }
    skipToEndElement(xmlRdr);
    return val;
}