java - Understanding the difference between Cursor API and Iterator API of StAX -


ok, when learning how process xml stax api. saw has 2 ways xml document parsed namely:

  • cursor api
  • iterator api
  • the cursor api use xmlstreamreader , it's next() , hasnext() methods.
    iterator api uses xmleventreader in same way above.

    the book sums iterator api in paragraph not quite descriptive. says use when want see event come next , based on value of xmlevent, can use xmlstreamreader skip or process upcoming event.

    i unable head around this. please explain how?

    sscce 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 = 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());         }     } } 

    maybe there more it, xmleventreader delivers xmlevent-objects give little more flexibility , functionality whereas xmlstreamreader not create these objects you.

    therefore, xmlstreamreader performing better while xmleventreader gives more functionality out of box.


    Comments

    Popular posts from this blog

    Perl - how to grep a block of text from a file -

    delphi - How to remove all the grips on a coolbar if I have several coolbands? -

    javascript - Animating array of divs; only the final element is modified -