3. SAX
• Sequential reading of XML files. Can not be
used to create XML documents.
• SAX provides an Event-Driven XML
Processing following the Push-Parsing
Model. What this model means is that in SAX
• Applications will register Listeners in the form
of Handlers to the Parser and will get notified
through Call-back methods.
• Here the SAX Parser takes the control over
Application thread by Pushing Events to the
Application.
5. // jdk1.4.1
import java.io.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
// using SAX
public class HowToListerSAX {
class HowToHandler extends DefaultHandler {
boolean title = false;
boolean url = false;
public void startElement(String nsURI, String strippedName,
String tagName, Attributes attributes)
throws SAXException {
if (tagName.equalsIgnoreCase("title"))
title = true;
if (tagName.equalsIgnoreCase("url"))
url = true;
}
public void characters(char[] ch, int start, int length) {
if (title) {
System.out.println("Title: " + new String(ch, start, length));
title = false;
}
else if (url) {
System.out.println("Url: " + new String(ch, start,length));
url = false;
}
}
}
public void list( ) throws Exception {
XMLReader parser =
XMLReaderFactory.createXMLReader
("org.apache.crimson.parser.XMLReaderImpl");
parser.setContentHandler(new HowToHandler( ));
parser.parse("howto.xml");
}
public static void main(String[] args) throws Exception {
new HowToListerSAX().list( );
}
}
I recommend not to use SAX.
6. DOM
● The DOM is an interface that exposes an XML
document as a tree structure comprised of
nodes.
● The DOM allows you to programmatically
navigate the tree and add, change and delete
any of its elements.
7. DOM
/ jdk1.4.1
import java.io.File;
import javax.xml.parsers.*;
import org.w3c.dom.*;
// using DOM
public class HowtoListerDOM {
public static void main(String[] args) {
File file = new File("howto.xml");
try {
DocumentBuilder builder =
DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(file);
NodeList nodes = doc.getElementsByTagName("topic");
for (int i = 0; i < nodes.getLength(); i++) {
Element element = (Element) nodes.item(i);
NodeList title = element.getElementsByTagName("title");
Element line = (Element) title.item(0);
System.out.println("Title: " + getCharacterDataFromElement(line));
NodeList url = element.getElementsByTagName("url");
line = (Element) url.item(0);
System.out.println("Url: " + getCharacterDataFromElement(line));
}
}
catch (Exception e) {
e.printStackTrace();
}
}
public static String getCharacterDataFromElement(Element e) {
Node child = e.getFirstChild();
if (child instanceof CharacterData) {
CharacterData cd = (CharacterData) child;
return cd.getData();
}
return "?";
}
}
I recommend not to use DOM.
8. STAX
● Streaming API for XML, simply called StaX, is
an API for reading and writing XML
Documents.
● StaX is a Pull-Parsing model. Application can
take the control over parsing the XML
documents by pulling (taking) the events from
the parser.
● The core StaX API falls into two categories
and they are listed below. They are
– Cursor API
– Event Iterator API
9. "Pull" vs. "Push" Style API
SAX PARSER YOUR APP
YOUR APP STAX PARSER
Stax is cool if you need the control over the XML
flow. Otherwise use JAXB.
10. EXEMPLE
<?xml version="1.0" encoding="UTF-8"?>
<config>
<mode>1</mode>
<unit>900</unit>
<current>1</current>
<interactive>1</interactive>
</config>
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.XMLEvent;
public class ConfigFileStaX2 {
private String configFile;
public void setFile(String configFile) {
this.configFile = configFile;
}
public static void main(String args[]) {
ConfigFileStaX2 read = new ConfigFileStaX2();
read.setFile("config.xml");
read.readConfig();
}
}
11. public void readConfig() {
try {
// First create a new XMLInputFactory
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
// Setup a new eventReader
InputStream in = new FileInputStream(configFile);
XMLEventReader eventReader = inputFactory.createXMLEventReader(in);
// Read the XML document
while (eventReader.hasNext()) {
XMLEvent event = eventReader.nextEvent();
if (event.isStartElement()) {
if (event.asStartElement().getName().getLocalPart() == ("mode")) {
event = eventReader.nextEvent();
System.out.println(event.asCharacters().getData());
continue;
}
if (event.asStartElement().getName().getLocalPart() == ("unit")) {
event = eventReader.nextEvent();
System.out.println(event.asCharacters().getData());
continue;
}
if (event.asStartElement().getName().getLocalPart() == ("current")) {
event = eventReader.nextEvent();
System.out.println(event.asCharacters().getData());
continue;
}
if (event.asStartElement().getName().getLocalPart() == ("interactive")) {
event = eventReader.nextEvent();
System.out.println(event.asCharacters().getData());
continue;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
12. PATTERN
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>Simple Atom Feed File</title>
<subtitle>Using StAX to read feed files</subtitle>
<link href="http://example.org/"/>
<updated>2006-01-01T18:30:02Z</updated>
<author>
<name>Feed Author</name>
<email>doofus@feed.com</email>
</author>
<entry>
<title>StAX parsing is simple</title>
<link href="http://www.devx.com"/>
<updated>2006-01-01T18:30:02Z</updated>
<summary>Lean how to use StAX</summary>
</entry>
</feed>
public interface ComponentParser {
public void parseElement(XMLStreamReader staxXmlReader) throws XMLStreamException;
}
13. public class AuthorParser implements ComponentParser{
public void parse(XMLStreamReader staxXmlReader) throws XMLStreamException{
// read name
StaxUtil.moveReaderToElement("name",staxXmlReader);
String name = staxXmlReader.getElementText();
// read email
StaxUtil.moveReaderToElement("email",staxXmlReader);
String email = staxXmlReader.getElementText();
// Do something with author data...
}
}
public class EntryParser implements ComponentParser {
public void parse(XMLStreamReader staxXmlReader) throws XMLStreamException{
// read title
StaxUtil.moveReaderToElement("title",staxXmlReader);
String title = staxXmlReader.getElementText();
// read link attributes
StaxUtil.moveReaderToElement("link",staxXmlReader);
// read href attribute
String linkHref = staxXmlReader.getAttributeValue(0);
// read updated
StaxUtil.moveReaderToElement("updated",staxXmlReader);
String updated = staxXmlReader.getElementText();
// read title
StaxUtil.moveReaderToElement("summary",staxXmlReader);
String summary = staxXmlReader.getElementText();
// Do something with the data read from StAX..
}
}
14. public class StaxParser implements ComponentParser {
private Map delegates;
…
public void parse(XMLStreamReader staxXmlReader) throws XMLStreamException{
for (int event = staxXmlReader.next(); event != XMLStreamConstants.END_DOCUMENT; event = staxXmlReader.next()) {
if (event == XMLStreamConstants.START_ELEMENT) {
String element = staxXmlReader.getLocalName();
// If a Component Parser is registered that can handle
// this element delegate…
if (delegates.containsKey(element)) {
ComponentParser parser = (ComponentParser) delegates.get(element);
parser.parse(staxXmlReader);
}
}
} //rof
}
}
InputStream in = this.getClass().getResourceAsStream("atom.xml");
XMLInputFactory factory = (XMLInputFactory) XMLInputFactory.newInstance();
XMLStreamReader staxXmlReader = (XMLStreamReader)
factory.createXMLStreamReader(in);
StaxParser parser = new StaxParser();
parser.registerParser("author",new AuthorParser());
parser.registerParser("entry",new EntryParser());
parser.parse(staxXmlReader);
15. JAXB
● JAXB is a Java standard that defines how
Java objects are converted to/from XML
(specified using a standard set of mappings.
● JAXB defines a programmer API for reading
and writing Java objects to / from XML
documents and a service provider which /
from from XML documents allows the
selection of the JAXB implementation
● JAXB applies a lot of defaults thus making
reading and writing of XML via Java very easy.
I recommend to use JAXB (or Stax if you
need more control).
17. XML Parser API Feature Summary
Feature StAX SAX DOM
API Type Pull, streaming Push, streaming In memory tree
Ease of Use High Medium High
XPath Capability Not supported Not supported Supported
CPU and Memory Good Good Varies
Efficiency
Read XML Supported Supported Supported
Write XML Supported Not supported Supported
18. NEXT
● JAXB + STAX
– http://www.javarants.com/2006/04/30/simple-
and-efficient-xml-parsing-using-jaxb-2-0/