File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes runtime Error using SAX parser Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "runtime Error using SAX parser" Watch "runtime Error using SAX parser" New topic
Author

runtime Error using SAX parser

Pooja Agarwal
Ranch Hand

Joined: May 19, 2004
Posts: 37
Hai..

I am using the following code to parse an XML called as order.xml.
I am using SAX parser to do so.
i have set the class path for JAXP.jar and Parser.jar in my class path.
The code is compiling fine but when i execute the code its giving an error.

C:\javaexamples>java ExtractorDriver order.xml
org.xml.sax.SAXException: System property org.xml.sax.driver not specified

It says "System property org.xml.sax.driver not specified".

Can any one let me know how to over come the error.

CODE
----
TextExtractor.java
------------------

import org.xml.sax.*;
import java.io.*;

public class TextExtractor implements ContentHandler {

private Writer out;

public TextExtractor(Writer out) {
this.out = out;
}

public void characters(char[] text, int start, int length)
throws SAXException {

try {
out.write(text, start, length);
}
catch (IOException e) {
throw new SAXException(e);
}

}

// do-nothing methods
public void setDocumentLocator(Locator locator) {}
public void startDocument() {}
public void endDocument() {}
public void startPrefixMapping(String prefix, String uri) {}
public void endPrefixMapping(String prefix) {}
public void startElement(String namespaceURI, String localName,
String qualifiedName, Attributes atts) {}
public void endElement(String namespaceURI, String localName,
String qualifiedName) {}
public void ignorableWhitespace(char[] text, int start,
int length) throws SAXException {}
public void processingInstruction(String target, String data){}
public void skippedEntity(String name) {}

} // end TextExtractor



ExtractorDriver.java
--------------------

import org.xml.sax.*;
import org.xml.sax.helpers.XMLReaderFactory;
import java.io.*;


public class ExtractorDriver {

public static void main(String[] args) {

if (args.length <= 0) {
System.out.println(
"Usage: java ExtractorDriver url"
);
return;
}

try {
XMLReader parser = XMLReaderFactory.createXMLReader();

Writer out = new OutputStreamWriter(System.out);
ContentHandler handler = new TextExtractor(out);
parser.setContentHandler(handler);

parser.parse(args[0]);

out.flush();
}
catch (Exception e) {
System.err.println(e);
}

}

}

Order.xml
---------

<?xml version="1.0" encoding="ISO-8859-1" ?>
- <Order>
<Customer id="c32">Chez Fred</Customer>
- <Product>
<Name>Birdsong Clock</Name>
<SKU>244</SKU>
<Quantity>12</Quantity>
<Price currency="USD">21.95</Price>
</Product>
- <ShipTo>
<Street>135 Airline Highway</Street>
<City>Narragansett</City>
<State>RI</State>
<Zip>02882</Zip>
</ShipTo>
<Subtotal currency="USD">263.40</Subtotal>
<Tax rate="7.0" currency="USD">18.44</Tax>
<Shipping method="USPS" currency="USD">8.95</Shipping>
<Total currency="USD">290.79</Total>
</Order>
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Pooja, welcome to Javaranch.

I need to move this thread to a more appropriate forum. This forum is for Servlets question. Your question is about SAX and XML.

Also there is a set of buttons under the Add Reply button when creating a thread or a reply. These buttons can really help with formatting. The one that says "CODE" can create tags that wrap around code and it will keep its format/tabbing/and spacing, to make nice and easy to read code.

Thanks

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Do you have a SAX parser implementation in your classpath?
If it is, and the XMLReaderFactory still complains about the system property missing, I guess you need to add it yourself (java ... -Dorg.xml.sax.driver=com.parser.impl.BlahBlah ... MyApplication).

PS. it's better to use the javax.xml.parsers.SAXParserFactory class from JAXP instead of the older XMLReaderFactory.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: runtime Error using SAX parser
 
Similar Threads
problem in parsing an XML using SAX parser.
SAX
SAX parser servlet help
Can any one help me
Problem with SAXParser when using with servlets.