javaxmlstax

Understanding XMLStreamReader and START_DOCUMENT


Consider the following XML file:

% cat test.xml
<?xml version="1.0" encoding="utf-8"?>
<root>
</root>

Why am I not getting a START_DOCUMENT event when using XMLStreamReader. Code is (lifted from):

% cat Demo.java
import java.io.FileReader;
import javax.xml.stream.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        XMLInputFactory factory = XMLInputFactory.newInstance();
        XMLStreamReader sr = factory.createXMLStreamReader(new FileReader("test.xml"));
        System.out.println(sr.getClass());

        while (sr.hasNext()) {
            int eventType = sr.next();

            if (eventType == XMLStreamReader.START_DOCUMENT) {
                System.out.println("Start Document" );
            } else if (eventType == XMLStreamReader.END_DOCUMENT) {
                System.out.println("End Document" );
            } else if (eventType == XMLStreamReader.END_ELEMENT) {
                System.out.println("End Element:    " + sr.getLocalName());
            } else if (eventType == XMLStreamReader.START_ELEMENT) {
                System.out.println("Start Element:  " + sr.getLocalName());
            }
        }
    }

}

Output on my side:

% javac Demo.java
% java Demo test.xml
class com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl
Start Element:  root
End Element:    root
End Document

Ref:

% java --version
openjdk 11.0.14 2022-01-18
OpenJDK Runtime Environment (build 11.0.14+9-post-Debian-1deb11u1)
OpenJDK 64-Bit Server VM (build 11.0.14+9-post-Debian-1deb11u1, mixed mode, sharing)

Solution

  • Turns out the documentation has it described:

    An XMLStreamReader instance is created with an initial event type START_DOCUMENT.

    So code should instead be:

    % cat Demo.java
    import java.io.FileReader;
    import javax.xml.stream.*;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            XMLInputFactory factory = XMLInputFactory.newInstance();
            XMLStreamReader sr = factory.createXMLStreamReader(new FileReader("test.xml"));
            System.out.println(sr.getClass());
    
            boolean hasNext;
            do {
                int eventType = sr.getEventType();
    
                if (eventType == XMLStreamReader.START_DOCUMENT) {
                    System.out.println("Start Document" );
                } else if (eventType == XMLStreamReader.END_DOCUMENT) {
                    System.out.println("End Document" );
                } else if (eventType == XMLStreamReader.END_ELEMENT) {
                    System.out.println("End Element:    " + sr.getLocalName());
                } else if (eventType == XMLStreamReader.START_ELEMENT) {
                    System.out.println("Start Element:  " + sr.getLocalName());
                }
                hasNext = sr.hasNext();
                if(hasNext) sr.next();
            } while( hasNext );
        }
    }
    

    The loop is easier to write using XMLEventReader:

    % cat Demo2.java
    import java.io.FileReader;
    import javax.xml.stream.*;
    import javax.xml.stream.events.*;
    
    public class Demo2 {
    
        public static void main(String[] args) throws Exception {
            XMLInputFactory factory = XMLInputFactory.newInstance();
            XMLEventReader er = factory.createXMLEventReader(new FileReader("test.xml"));
            System.out.println(er.getClass());
    
            while(er.hasNext()) {
                XMLEvent xmlEvent = er.nextEvent();
                int eventType = xmlEvent.getEventType();
                if (eventType == XMLStreamConstants.START_DOCUMENT) {
                    System.out.println("Start Document" );
                } else if (eventType == XMLStreamConstants.END_DOCUMENT) {
                    System.out.println("End Document" );
                } else if (eventType == XMLStreamConstants.END_ELEMENT) {
                    System.out.println("End Element:    " + xmlEvent.asEndElement().getName());
                } else if (eventType == XMLStreamConstants.START_ELEMENT) {
                    System.out.println("Start Element:  " + xmlEvent.asStartElement().getName());
                }
            }
        }
    }