Searching...
Wednesday, 19 September 2012

Java XML SAX Parser Example

03:14

 

SAX Parser Example

SAX parser using callback function of org.xml.sax.helpers.DefaultHandler to informs clients of the XML document structure. We have to extend DefaultHandler and override few methods to achieve xml parsing.

The methods to override are

  • startDocument() and endDocument() – Method called at the start and end of an XML document.
  • startElement() and endElement() – Method called at the start and end of a document element.
  • characters() – Method called with the text contents in between the start and end tags of an XML document element.
The below example shows the uses of DefaultHandler to parse and XML document.

SAX Parser Classes

SAXParserFactory –> Defines a factory API that enables applications to configure and obtain a SAX based parser to parse XML documents.
SAXParser ->Defines the API that wraps an XMLReader implementation class.
DefaultHandler –>This class provides default implementations for all of the callbacks in the four core SAX2 handler classes

Sample xml file

<Order>
<Ordernumber Number="1111">
<OrderedItem1>Pepsi</OrderedItem1>
<OrderitemPrice>100</OrderitemPrice>
<OrderitemTax>10</OrderitemTax>
<OrderCoupon>NEWYEAR</OrderCoupon>
</Ordernumber>
</Order>

Create a Java file

package mycollectiontest;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class XMLParser {

public static void main(String[] args) {
try {

SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();

DefaultHandler handler = new DefaultHandler() {
boolean borderedItem1 = false;
boolean borderitemPrice = false;
boolean borderitemTax = false;
boolean borderCoupon = false;

public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if (qName.equalsIgnoreCase("OrderedItem1")) {
borderedItem1 = true;
}
if (qName.equalsIgnoreCase("OrderitemPrice")) {
borderitemPrice = true;
}
if (qName.equalsIgnoreCase("OrderitemTax")) {
borderitemTax = true;
}
if (qName.equalsIgnoreCase("OrderCoupon")) {
borderCoupon = true;
}
}

public void endElement(String uri, String localName, String qName) throws SAXException {
}

public void characters(char ch[], int start, int length) throws SAXException {
if (borderedItem1) {
System.out.println("OrderedItem1 : " + new String(ch, start, length));
borderedItem1 = false;
}
if (borderitemPrice) {
System.out.println("OrderitemPrice : " + new String(ch, start, length));
borderitemPrice = false;
}
if (borderitemTax) {
System.out.println("OrderitemTax : " + new String(ch, start, length));
borderitemTax = false;
}
if (borderCoupon) {
System.out.println("OrderCoupon : " + new String(ch, start, length));
borderCoupon = false;
}
}
};
saxParser.parse("order.xml", handler);
} catch (Exception e) {
e.printStackTrace();
}
}

}

Output

OrderedItem1 : Pepsi
OrderitemPrice : 100
OrderitemTax : 10
OrderCoupon : NEWYEAR

Reference

docs.oracle.com

0 comments: