Pages

Monday, March 7, 2011

SAX and DOM Parsers

SAX:Simple API for XML.
1)SAX is read-only parser.We cann't delete,update,create/generate new XML.
More over SAX reads the data in a sequential order.
SAX is based on Event-Driven Model.
An Event-Driven Model implies that parsing an XML results into following events.
(1)start of document
(2)End of document
(3)Start of Element
(4)End of Element
(5)Character Data is started.
SAX occupies less memory as it loads one element at any time.

Consider the example below: Jars required are Xerces.jar
employees.xml
-------------
<?xml version="1.0" encoding="UTF-8"?>
< employees>
< employee id="768">
< ename>keely</ename>
< email>dileepkeely@gmail.com</email>
< phone>09880614099</phone>
< /employee>
< /employees>

XMLReader.java
--------------
package com.keelys.xml.reader;
import com.keely.handler.*;
import java.io.IOException;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.XMLReaderFactory;
public class XMLReader {
public static void main(String[] args) {
try {
org.xml.sax.XMLReader reader = XMLReaderFactory
.createXMLReader("org.apache.xerces.parsers.SAXParser");
reader.setContentHandler(new HandlerEvent());
try {
reader.parse("employees.xml");
} catch (IOException e) {
e.printStackTrace();
}
} catch (SAXException e) {
e.printStackTrace();
}
}
}
Note:
"org.apache.xerces.parsers.SAXParser" :It is the main Xerces SAX parser class.
It uses abstract SAX parser with a document scanner,DTD and attempt to create
an XMLReader.
"XMLReader": Interface for reading an XML document using callbacks

HandlerEvent.java
-----------------
package com.keely.handler;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class HandlerEvent extends DefaultHandler {
public void startDocument() {
System.out.println("Start of document");
}

public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
for (int i = 0; i < attributes.getLength(); i++) {
System.out.println(attributes.getType(0) + "\t"
+ attributes.getLocalName(0) + "\t"
+ attributes.getValue(i));
}
System.out.println(qName);
}

public void characters(char[] ch, int start, int length)
throws SAXException {
String characterData = new String(ch, start, length);
System.out.println(characterData);
}

public void endDocument() {
System.out.println("end document");
}
}

o/p:
Start of document
employees

CDATA id 768
employee

ename
keely

email
dileepkeely@gmail.com

phone
09880614099

end document



DOM:Document Object Model.

No comments:

Post a Comment