Solved

Stax Parser

Posted on 2004-11-01
1,521 Views
Last Modified: 2013-11-23
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
0
Question by:sudhakar_koundinya
    3 Comments
     
    LVL 3

    Expert Comment

    by:vikraman_b
    HI..
    i too aware of it..
    check this link..
    it will be helpful
    http://www.sys-con.com/story/?storyid=45083&DE=1
    0
     
    LVL 3

    Accepted Solution

    by:
    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
     
    LVL 14

    Author Comment

    by:sudhakar_koundinya
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
    Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
    The viewer will learn how to implement Singleton Design Pattern in Java.
    This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

    875 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now