Browse DevX
Sign up for e-mail newsletters from DevX


Choose Your Java XML Parser  : Page 2

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


Oracle Parser
I used the beta 2.1.0 version of the parser included with the Oracle XML tools for this article. The Oracle parser implements DOM Level 1, and SAX 1.0 and 2.0. It has a partial implementation of DOM Level 2 and includes APIs for XSLT. It supports Schemas through the oracle.xml.parser.schema package.

The DOM class shown earlier requires relatively few changes to compile and run with the Oracle parser. Change the second import line so it refers to the Oracle parser. The parse() method, expects a string containing a URL, not a physical path and filename. Alternately, you can pass the parse() method a URL object.

import org.w3c.dom.*;
import oracle.xml.parser.v2.DOMParser;

public class DOM
    public static void main(String[] args) 

        try {
            DOMParser parser = new DOMParser();   
            String url = "file://C|/xml/code/" + args[0];
            Document doc = parser.getDocument();

            NodeList nodes = doc.getElementsByTagName("servlet");
            System.out.println("There are " + nodes.getLength() +
" elements."); } catch (Exception ex) { System.out.println(ex); } } }

You must make two changes for the SAX version. The import statements would be:

import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;
import oracle.xml.parser.v2.SAXParser;
The initiation of the parser would become:
SAXParser parser = new SAXParser();
String url = "file://C|/xml/code/" + args[0];

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