Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Parsing xml file using SAX

 
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Men call me Jim. Women look past me to this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic