HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"StAX: Java's XML Pull Parser Specification"
Vol. 9, Issue 6, p. 48

	



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();

 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.