Sunday, August 19, 2007

Parsing & Binding XML

XML has arrived. Configuration files, application file formats, even database access layers make use of XML-based documents.Here is an accessible introduction to the two most widely used APIs
Application XML:

XML Processing:

Three traditional techniques for processing XML files are:

* Using a programming language and the SAX API.
* Using a programming language and the DOM API.
* Using a transformation engine and a filter


The Xerces Java Parser 1.4.4 supports the XML 1.0 recommendation and contains advanced parser functionality, such as support for the W3C's XML Schema recommendation version 1.0, DOM Level 2 version 1.0, and SAX Version 2, in addition to supporting the industry-standard DOM Level 1 and SAX version 1 APIs and supports Xml-dtd validation. For
more Info

DOM Vs SAX Parsing:

Dom create the dom tree by parsing the given xml where as SAX Creates the Event Structure as shown below

DOM Example:

// Create a Xerces DOM Parser
DOMParser parser = new DOMParser();
// Parse the Document and traverse the DOM
Document document = parser.getDocument();
traverse (document);
// Traverse DOM Tree and print elements names
private void traverse (Node node) {
int type = node.getNodeType();
if (type == Node.ELEMENT_NODE)
System.out.println (node.getNodeName());
NodeList children = node.getChildNodes();
if (children != null) {
for (int i=0; i< color="#005e00">getLength
(); i++)
traverse (children.item(i));

SAX Example:

public class BasicSAX extends org.xml.sax.helpers.DefaultHandler {
public BasicSAX (String xmlFile) {
//Create a Xerces SAX Parser
SAXParser parser = new SAXParser();
//Set Content Handler
parser.setContentHandler (this);
//Parse the Document

// Start Element Event Handler
public void startElement (String uri, String local, String qName, Attributes atts) {
System.out.println (local);



The Java API for XML Processing (JAXP) supports processing of XML documents using DOM, SAX, and XSLT. JAXP enables applications to parse and transform XML documents independent of a particular XML processing implementation.

SAX with JAXP:

import java.xml.parsers.*
import javax.xml.parsers.SAXParserFactory
import org.xml.sax.*
SAXParserFactory factory =SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
parser.parse("sample.xml", handler);
// can also parse InputStreams, Files, and
// SAX input sources.


import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.*
DocumentBuilderFactory factory =DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse("sample.xml");
// can also parse InputStreams, Files, and
// SAX input sources.


import java.xml.transform.*;
import javax.xml.transform.TransformerFactory;
Transformer transformer;
TransformerFactory factory =TransformerFactory.newInstance();
// Create a transformer for a particular stylesheet.transformer = factory.newTransformer(new StreamSource(stylesheet));
// Transform the source xml to System.out.
transformer.transform(new StreamSource(sourceId),new StreamResult(System.out));

No comments: