Two Laptop Bag
The moose likes XML and Related Technologies and the fly likes Parsing xml file using SAX Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Parsing xml file using SAX" Watch "Parsing xml file using SAX" New topic

Parsing xml file using SAX

Bhasker Reddy
Ranch Hand

Joined: Jun 13, 2000
Posts: 176
I am trying to parse a small xml file using SAX parser.
I am using the following code
package com.nortel.nimo.logic;
import org.xml.sax.*;
import org.xml.sax.helpers.XMLReaderFactory;
import org.xml.sax.helpers.DefaultHandler;
import org.apache.xerces.parsers.SAXParser;

public class OutputGenerator extends DefaultHandler{
private File billFile;
private PrintWriter out;

public static void main (String args[])throws Exception{
OutputGenerator outGen = new OutputGenerator(new File("C:/nortelsax/test/acct.xml"), new PrintWriter(new BufferedWriter(new FileWriter("C:/nortelsax/test/output.xml"))));
//System.setProperty(org.xml.sax.helpers.XMLReaderFactory, com.nortel.nimo.logic.OutputGenerator);


public OutputGenerator(File _billFile, PrintWriter _out) {
this.billFile = _billFile;
this.out = _out;
// OutputGenerator handler = new OutputGenerator();
createPreprocessedOutput(this.billFile, this.out);

public OutputGenerator(){


public static synchronized String createPreprocessedOutput(File billFile, PrintWriter out) {

try {
XMLReader xr = XMLReaderFactory.createXMLReader();
OutputGenerator handler = new OutputGenerator();

FileReader r = new FileReader(billFile);
xr.parse(new InputSource(r));

System.out.println(billFile + " is well-formed.");
catch (SAXException e) {
System.out.println(billFile + " is not well-formed." + e.getMessage());
catch (IOException e) {
System.out.println("Due to an IOException, the parser could not check " + billFile);

return "success";

public void startDocument ()
System.out.println("Start document");

public void endDocument ()
System.out.println("End document");

public void startElement (String uri, String name,
String qName, Attributes atts)
if ("".equals (uri))
System.out.println("Start element: " + qName);
System.out.println("Start element: {" + uri + "}" + name);

public void endElement (String uri, String name, String qName)
if ("".equals (uri))
System.out.println("End element: " + qName);
System.out.println("End element: {" + uri + "}" + name);

I am getting an error, it says system property org.xml.sax.driver not specified. Is driver something that we need to include, wHAT IS DRIVER
Can anyone explain it to me.

Bhasker Reddy
Kamlesh Sangani

Joined: Feb 28, 2004
Posts: 22
Use this to create your parser

XMLReader xr = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");

or you can also specify which parser you want to use at runtime using org.xml.sax.driver property.

i hope it helps...

Lets Java !<br />Kamlesh
Lasse Koskela

Joined: Jan 23, 2002
Posts: 11962
Do you have a parser implementation in your classpath?
J2SE 1.4 has one built-in (Apache Crimson), but if you're using 1.3 then you need to download one (I'd suggest Apache Xerces).

Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
I agree. Here's the link:
subject: Parsing xml file using SAX
It's not a secret anymore!