Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A very basic JAXP example

 
Michael Zhou
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all experts,
Now I am studying the JAXP, and through some studies, I have theoratically come to adhering to the idea that JAXP allows other vendor-specific XML parsers to be plugged into it, and that we do not have to change the code, when we change the underlying XML parser (for example, from Apache's Xerces to IBM's xml4j). However, in the following code that I read when surfing the IBM's developer network, the vendor-specific API code does explicitly appears in the source code, that sparks a lot of confusion, could you clarify that?

******************************************************************************* *******************
import java.io.File;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
// JAXP
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.SAXParser;

// SAX
import org.xml.sax.AttributeList;
import org.xml.sax.HandlerBase;
import org.xml.sax.SAXException;

public class TestSAXParsing {
public static void main(String[] args) {
try {
if (args.length != 1) {
System.err.println ("Usage: java TestSAXParsing [filename]");
System.exit (1);
}
// Get SAX Parser Factory
SAXParserFactory factory = SAXParserFactory.newInstance();
// Turn on validation, and turn off namespaces
factory.setValidating(true);
factory.setNamespaceAware(false);
SAXParser parser = factory.newSAXParser();
parser.parse(new File(args[0]), new MyHandler());
} catch (ParserConfigurationException e) {
System.out.println("The underlying parser does not support " +
" the requested features.");
} catch (FactoryConfigurationError e) {
System.out.println("Error occurred obtaining SAX Parser Factory.");
} catch (Exception e) {
e.printStackTrace();
}
}
}
class MyHandler extends HandlerBase {
// SAX callback implementations from DocumentHandler, ErrorHandler, etc.
}

Thanks a lot!
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since this is an XML question and not specific to web services, I'm moving this thread over to our XML forum. Please continue the discussion over there.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic