Search code examples
javaxmliteratorcursorstax

Understanding the difference between Cursor API and Iterator API of StAX


Ok, when learning how to process XML with StAX API. I saw that it has 2 ways to get the XML document parsed namely:

  • Cursor API
  • Iterator API
  • The Cursor API is where you use XMLStreamReader and it's next() and hasNext() methods.
    The Iterator API uses XMLEventReader in much the same way as above.

    The book sums up the Iterator API in just a paragraph which is not quite descriptive. It says that you use this when you want to see what event will come up next and based on the value of XMLEvent, you can use the XMLStreamReader to skip or process the upcoming event.

    I am unable to get my head around this. Could someone please explain HOW?

    SSCCE for Cursor API

    import javax.xml.stream.*;
    import javax.xml.stream.events.*;
    import java.io.*;
    
    public class StaxCursorDemo{
        public static void main(String[] args){
            try{
                XMLInputFactory inputFactory = XMLInputFactory.newInstance();
                InputStream input = new FileInputStream(new File("helloWorld.xml"));
                XMLStreamReader xmlStreamReader = inputFactory.createXMLStreamReader(input);
    
                while(xmlStreamReader.hasNext()){
                    int event = xmlStreamReader.next();
                    if(event == XMLStreamConstants.START_DOCUMENT){
                        System.out.println("Beginning parsing of document");
                    }
                    if(event == XMLStreamConstants.END_DOCUMENT){
                        System.out.println("Ending parsing of document");
                    }
                    if(event == XMLStreamConstants.COMMENT){
                        System.out.println("NOTE: " + xmlStreamReader.getText());
                    }
                    if(event == XMLStreamConstants.START_ELEMENT){
                        System.out.println("Beginning Element: " + xmlStreamReader.getLocalName());
                        for(int i = 0;i<xmlStreamReader.getAttributeCount();i++){
                            System.out.println("Attribute is: " + xmlStreamReader.getAttributeLocalName(i));
                            System.out.println("Attribute value is: " + xmlStreamReader.getAttributeValue(i));
                        }
                    }
                    if(event == XMLStreamConstants.END_ELEMENT){
                        System.out.println("End Element: ");
                    }
                    if(event == XMLStreamConstants.CHARACTERS){
                        System.out.println("Value: " + xmlStreamReader.getText());
                    }
                }
            }catch(FactoryConfigurationError e){
                System.out.println(e.getMessage());
            }catch(XMLStreamException e){
                System.out.println(e.getMessage());
            }catch(IOException e){
                System.out.println(e.getMessage());
            }
        }
    }
    

    Solution

  • Maybe there is more to it, but XMLEventReader delivers XMLEvent-Objects that give you a little more flexibility and functionality whereas XMLStreamReader does not create these Objects for you.

    Therefore, XMLStreamReader is performing better while XMLEventReader gives you some more functionality out of the box.