dcsimg
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Choose Your Java XML Parser  : Page 3

Parsers differ significantly in performance, reliability, and conformance to standards. This article compares the Xerces, Oracle and Sun parsers.


advertisement

WEBINAR:

On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning


Sun Parser
Sun packages its XML APIs as the Java API for XML Processing (JAXP). I used an early relase latest version (1.1). Like Oracle, Sun incorporates support for DOM, SAX, Schema and XSL into its parser. The XML Parser is based on the Project X parser from Sun and the XSLT processor is actually Xalan from Apache. Using factory classes, JAXP allows you to plug in any conforming XML or XSL parser, thus creating a standard mechanism for Java applications to interact with XML parsers. The parser supports SAX 2.0, DOM Level 2 and XSLT 1.0. You need to add three jar files--jaxp.jar, crimson.jar, and xalan.jar--to your CLASSPATH..

Import the following three lines to use the DOM API with JAXP. Note that the JAXP specific classes begin with "javax."



import org.w3c.dom.*;
import javax.xml.parsers.*;
import java.io.*;

Sun's strategy of using factory classes makes the process of initiating the parser a bit different. Here are the necessary lines:

DocumentBuilderFactory dbf =
DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(args[0]);

The first line creates a new DocumentBuilderFactory instance. By default, the DocumentBuilderFactory uses the built-in XML parser that comes with JAXP,
but you can change the parser by setting the system property javax.xml.parsers.DocumentBuilderFactory. You can pre-configure the parser using factory class methods like setValidating() and setNamespaceAware(). After you have the factory, you create a DocumentBuilder and invoke its parse() method to parse a document.

You also use a factory class to create a SAX parser. Use the javax.xml.parsers.SAXParserFactory system property to set the default parser. The import lines would change to the following:

import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;
import javax.xml.parsers.*;

There are several ways of initiating the parsing. One way would be to use the factory to get the parser directly like this:

SAX SAXHandler = new SAX();
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sparser = spf.newSAXParser();
sparser.parse(args[0], SAXHandler);

Note that the preceding example passes the SAXHandler object as a parameter to the parse() method. This way the parser "knows" where the event handlers for the various SAX events are.

You can also interact indirectly with a SAX parser through the SAX2 compliant XMLReader interface. For example:

SAX SAXHandler = new SAX();
SAXParserFactory spf = SAXParserFactory.newInstance();
XMLReader xmlReader = null;
try {
    SAXParser saxParser = spf.newSAXParser();
    xmlReader = saxParser.getXMLReader();
} catch (Exception ex) {
    System.out.println(ex);
}

xmlReader.setContentHandler(SAXHandler);
xmlReader.setErrorHandler(SAXHandler);

try {
    xmlReader.parse(args[0]);
} catch (SAXException ex) {
    System.out.println(ex);
}


Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date