Posted in Books

Parsing XML document using SAX Parser

import java.io.IOException;

import javax.xml.parsers.SAXParser;

import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;

import org.xml.sax.ContentHandler;

import org.xml.sax.ErrorHandler;

import org.xml.sax.InputSource;

import org.xml.sax.Locator;

import org.xml.sax.SAXException;

import org.xml.sax.SAXParseException;

import org.xml.sax.XMLReader;

public class SAX_Parser_Example {

int batchSize = 0;

static public void main(String[] arg) {

// Give the file path of your system.

String filename = “C:\\Users\\WS\\POC_SPVTG\\POC_23Aug\\PEIS_DataSample.xml”;

// Create a new factory that will create the parser.

SAXParserFactory spf = SAXParserFactory.newInstance();

// Create the XMLReader to be used to parse the document.

long startTime = System.currentTimeMillis();

XMLReader reader = null;

try {

SAXParser parser = spf.newSAXParser();

reader = parser.getXMLReader();

} catch (Exception e) {

System.err.println(e);

System.exit(1);

}

// Specify the error handler and the content handler.

reader.setErrorHandler(new MyErrorHandler());

reader.setContentHandler(new MyContentHandler());

// Use the XMLReader to parse the entire file.

try {

InputSource is = new InputSource(filename);

reader.parse(is);

} catch (SAXException e) {

System.exit(1);

} catch (IOException e) {

System.err.println(e);

System.exit(1);

}

long endTime = System.currentTimeMillis();

System.out.println(“Time taken to complete the parsing of document”+(endTime-startTime));

}

}

class MyContentHandler implements ContentHandler {

private Locator locator;

private StringBuffer curCharValue = new StringBuffer(1024);

/**

* The name and of the SAX document and the current location within the

* document.

*/

private int batchCount =0;

public void setDocumentLocator(Locator locator) {

this.locator = locator;

}

/** The parsing of a document has started.. */

public void startDocument() {

}

/** The parsing of a document has completed.. */

public void endDocument() {

}

/** The start of a namespace scope */

public void startPrefixMapping(String prefix, String uri) {

}

/** The end of a namespace scope */

public void endPrefixMapping(String prefix) {

}

/** The opening tag of an element. */

public void startElement(String namespaceURI, String localName,

String qName, Attributes atts) {

System.out.println(“<“+qName+”>”);

}

 

/** The closing tag of an element. */

public void endElement(String namespaceURI, String localName, String qName) {

System.out.println(“</”+qName+”>”);

}

/** Character data. */

public void characters(char[] ch, int start, int length) {

showCharacters(ch, start, length);

}

/** Ignorable whitespace character data. */

public void ignorableWhitespace(char[] ch, int start, int length) {

showCharacters(ch, start, length);

}

/** Processing Instruction */

public void processingInstruction(String target, String data) {

}

/** A skipped entity. */

public void skippedEntity(String name) {

}

/**

* Internal method to format arrays of characters so the special whitespace

* characters will show.

*/

public void showCharacters(char[] ch, int start, int length) {

for (int i = start; i < start + length; i++)

switch (ch[i]) {

case ‘\n’:

break;

case ‘\r’:

break;

case ‘\t’:

break;

default:

System.out.print(ch[i]);

curCharValue.append(ch, start, length);

break;

}

// System.out.println(“\””);

}

}

class MyErrorHandler implements ErrorHandler {

public void warning(SAXParseException e) throws SAXException {

show(“Warning”, e);

throw (e);

}

public void error(SAXParseException e) throws SAXException {

show(“Error”, e);

throw (e);

}

public void fatalError(SAXParseException e) throws SAXException {

show(“Fatal Error”, e);

throw (e);

}

private void show(String type, SAXParseException e) {

System.out.println(“System ID: ” + e.getSystemId());

}

}

 

Advertisements

4 thoughts on “Parsing XML document using SAX Parser

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s