Stax Parser

Hello Guys,

Have you heard abt STAX parser

http://www.google.co.in/search?hl=en&q=%22stax+parser%22++java&btnG=Search&meta=

If any body have used this parser, please share your experiences.

If possible some examples are appreciated.

Thanks
Sudhakar
LVL 14
sudhakar_koundinyaAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
vikraman_bConnect With a Mentor Commented:
In the above link u can check this code......

Listing 1: Reading with the XMLStreamReader

        while(sr.hasNext()){
            sr.next();
            if(sr.getEventType() == XMLStreamConstants.START_ELEMENT){
                if(sr.getLocalName().equals("sometext") &&
      sr.getNamespaceURI().equals("http://www.example.com/foons")){
                    // get all the text content of <sometext> and store in
                              someText
                              variable
                    someText = sr.getElementText();
                } else if(sr.getLocalName().equals("bar")
                       &&
   sr.getNamespaceURI().equals("http://www.example.com/barns")){
                    // get the value of the ratio attribute
                    ratio = sr.getAttributeValue(null,"ratio");
                }
            } else {
                // ignore event
            }
        }


Listing 2: Reading with the XMLEventReader

        while(er.hasNext()){
            XMLEvent event = er.nextEvent();
            if(event.getEventType() == XMLStreamConstants.START_ELEMENT){
                StartElement se = event.asStartElement();
                if(se.getName().equals(new
                        QName("http://www.example.com/foons","sometext"))){
                    someText = se.getElementText();
                } else if(se.getName().equals(new
                         QName("http://www.example.com/barns","bar"))){
                    Attribute ratioAttr = se.getAttributeByName(new
                              QName("ratio"));
                    if(ratioAttr != null){
                        ratio = ratioAttr.getValue();
                    }
                }
            } else {
                // ignore event
            }
        }


Listing 3: Writing with the XMLStreamWriter

XMLOutputFactory factory = XMLOutputFactory.newInstance();
        factory.setProperty("javax.xml.stream.isPrefixDefaulting",

Boolean.TRUE);
       
         XMLStreamWriter sw = factory.createXMLStreamWriter(new
         FileOutputStream(destination),"UTF-8");
        sw.writeStartDocument();
        sw.writeStartElement("http://www.example.com/foons", "foo");
        sw.writeStartElement("http://www.example.com/foons", "sometext");
        sw.writeCharacters(someText);
        sw.writeEndElement();
        sw.writeStartElement("http:/www.example.com/barns", "bar");
        sw.writeAttribute("ratio",ratio);
        sw.writeEndElement();
        sw.writeEndElement();
        sw.writeEndDocument();
        sw.close();


Listing 4: Writing with the XMLEventWriter

        XMLOutputFactory factory = XMLOutputFactory.newInstance();
        XMLEventFactory eventFac = XMLEventFactory.newInstance();
        factory.setProperty("javax.xml.stream.isPrefixDefaulting",Boolean.TRUE);
        XMLEventWriter ew =  factory.createXMLEventWriter(new
            FileOutputStream(destination),"UTF-8");
        ew.add(eventFac.createStartDocument());
        ew.add(eventFac.createStartElement(null,"http://www.example.com/foons",
            "foo"));
        ew.add(eventFac.createStartElement(null,"http://www.example.com/foons",
            "sometext"));
        ew.add(eventFac.createCharacters(someText));
        ew.add(eventFac.createEndElement(null,"http://www.example.com/foons",
            "sometext"));
        ew.add(eventFac.createStartElement(null,"http:/www.example.com/barns",
            "bar"));
        ew.add(eventFac.createAttribute("ratio",ratio));
        ew.add(eventFac.createEndElement(null,"http://www.example.com/barns",
            "bar"));
        ew.add(eventFac.createEndElement(null,"http://www.example.com/foons",
            "foo"));
        ew.add(eventFac.createEndDocument());
        ew.close();
0
 
vikraman_bCommented:
HI..
i too aware of it..
check this link..
it will be helpful
http://www.sys-con.com/story/?storyid=45083&DE=1
0
 
sudhakar_koundinyaAuthor Commented:
Friend,

I have seen that link. See above I have already given google's link for this

I want your experience in using STAX. In other words how it is efficient when compared with DOM/SAX

How can we handle XSLT with stax and so on

thanks
Sudhakar
0
All Courses

From novice to tech pro — start learning today.