*
The moose likes XML and Related Technologies and the fly likes A very basic JAXP example 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 "A very basic JAXP example" Watch "A very basic JAXP example" New topic
Author

A very basic JAXP example

Michael Zhou
Greenhorn

Joined: Jan 27, 2005
Posts: 12
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

Joined: Jan 23, 2002
Posts: 11962
    
    5
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.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A very basic JAXP example