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: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 ww .  j  av  a  2 s  .  com

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

public static void main(String[] args) throws Exception {
    XMLInputFactory xif = XMLInputFactory.newFactory();

    FileInputStream xml = new FileInputStream("input.xml");
    XMLStreamReader xsr = xif.createXMLStreamReader(xml);
    xsr.nextTag(); // Advance to "Persons" tag
    xsr.nextTag(); // Advance to "Person" tag

    JAXBContext jc = JAXBContext.newInstance(Person.class);
    Unmarshaller unmarshaller = jc.createUnmarshaller();
    List<Person> persons = new ArrayList<Person>();
    while (xsr.hasNext() && xsr.isStartElement()) {
        Person person = (Person) unmarshaller.unmarshal(xsr);
        persons.add(person);//from w w  w .  j  a  va2 s  .  c om
        xsr.nextTag();
    }

    for (Person person : persons) {
        System.out.println(person.getName());
    }
}

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   w  w w. j  a  v a  2 s  .  c o  m*/
        xsr.next();
    }
}

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 . java 2s .  co m
    if (reader.getEventType() == XMLStreamReader.END_DOCUMENT) {
        return Optional.empty();
    } else {
        return Optional.of(reader.getName().getLocalPart());
    }
}

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);//  ww w . ja v a2s  . c o  m
        } else if (eventType == XMLStreamConstants.END_ELEMENT) {
            return;
        }
    }
}

From source file:Main.java

private static int countEvents(XMLStreamReader reader) throws XMLStreamException {
    int counter = 1;
    while (reader.hasNext()) {
        reader.next();/* www.j a v a 2s  .  c  o  m*/
        counter++;
    }
    return counter;
}

From source file:Main.java

public static void skipToStart(XMLStreamReader xmlRdr, String elementName) throws XMLStreamException {
    while (xmlRdr.hasNext()) {
        if (xmlRdr.isStartElement() && xmlRdr.getLocalName().equals(elementName)) {
            break;
        }//from   ww  w .  j  a v  a2  s. c  o  m
        xmlRdr.next();
    }
}

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;
        }/*from ww w .  j a  v a2 s .  com*/
        if (code == XMLStreamReader.START_ELEMENT && reader.getLocalName().equals(X)) {
            return true;
        }
    }
    return false;
}

From source file:Main.java

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