Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Parsing xml file using SAX

 
Bhasker Reddy
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to parse a small xml file using SAX parser.
I am using the following code
package com.nortel.nimo.logic;
import java.io.*;
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();
xr.setContentHandler(handler);
xr.setErrorHandler(handler);

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);
else
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);
else
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
HOW DO WE INCLUDE IT??
Can anyone explain it to me.
 
Kamlesh Sangani
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic