www.alphaworks.ibm.comwww.ibm.com/developerwww.ibm.com

Home

XML4J Readme
Xerces Readme
Installation

Samples
API JavaDoc
XNI Manual
FAQs

Features
Properties

Release Info
Migration details
Limitations
Report a Bug

Questions
 

Answers
 
How do I create a DOM parser?
 

You can create a DOM parser by using the Java APIs for XML Processing (JAXP). The following source code shows how:

import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

  ...

String xmlFile = "file:///xerces-4_0_5/data/personal.xml"; 
try {
    DocumentBuilderFactory factory = 
        DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.parse(xmlFile);
}
catch (FactoryConfigurationError e) {
    // unable to get a document builder factory
} 
catch (ParserConfigurationException e) {
    // parser was unable to be configured
catch (SAXException e) {
    // parsing error
} 
catch (IOException e) {
    // i/o error
}

How do I create a SAX parser?
 

You can create a SAX parser by using the Java APIs for XML Processing (JAXP). The following source code shows how:

import java.io.IOException; 
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

  ...

String xmlFile = "file:///xerces-4_0_5/data/personal.xml"; 
try {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    SAXParser parser = factory.newSAXParser();
    DefaultHandler handler = /* custom handler class */;
    parser.parse(xmlFile, handler);
} 
catch (FactoryConfigurationError e) {
    // unable to get a document builder factory
} 
catch (ParserConfigurationException e) {
    // parser was unable to be configured
catch (SAXException e) {
    // parsing error
} 
catch (IOException e) {
    // i/o error
}

How do handle errors?
 

You should register an error handler with the parser by supplying a class which implements the org.xml.sax.ErrorHandler interface. This is true regardless of whether your parser is a DOM based or SAX based parser.

You can register an error handler on a DocumentBuilder created using JAXP like this:

import javax.xml.parsers.DocumentBuilder;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

ErrorHandler handler = new ErrorHandler() {
    public void warning(SAXParseException e) throws SAXException {
        System.err.println("[warning] "+e.getMessage());
    }
    public void error(SAXParseException e) throws SAXException {
        System.err.println("[error] "+e.getMessage());
    }
    public void fatalError(SAXParseException e) throws SAXException {
        System.err.println("[fatal error] "+e.getMessage());
	throw e;
    }
};

DocumentBuilder builder = /* builder instance */;
builder.setErrorHandler(handler);

You can also register an error handler on a SAXParser using JAXP like this:

import javax.xml.parsers.SAXParser;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

ErrorHandler handler = new ErrorHandler() {
    public void warning(SAXParseException e) throws SAXException {
        System.err.println("[warning] "+e.getMessage());
    }
    public void error(SAXParseException e) throws SAXException {
        System.err.println("[error] "+e.getMessage());
    }
    public void fatalError(SAXParseException e) throws SAXException {
        System.err.println("[fatal error] "+e.getMessage());
	throw e;
    }
};

SAXParser parser = /* parser instance */;
parser.getXMLReader().setErrorHandler(handler);

How can I control the way that entities are represented in the DOM?
 

The DOM Level 3 feature create-entity-ref-nodes (or corresponding Xerces http://apache.org/xml/features/dom/create-entity-ref-nodes feature) control how entities appear in the DOM tree. When one of those features is set to true (the default), an occurance of an entity reference in the XML document will be represented by a subtree with an EntityReference node at the root whose children represent the entity expansion.

If the feature is false, an entity reference in the XML document is represented by only the nodes that represent the entity expansion.

In either case, the entity expansion will be a DOM tree representing the structure of the entity expansion, not a text node containing the entity expansion as text.


Why does "non-validating" not mean "well-formedness checking only"?
 

Using a "non-validating" parser does not mean that only well-formedness checking is done! There are still many things that the XML specification requires of the parser, including entity substitution, defaulting of attribute values, and attribute normalization.

This table describes what "non-validating" really means for Xerces-J parsers. In this table, "no DTD" means no internal or external DTD subset is present.

  non-validating parsers  validating parsers 
  DTD present  no DTD  DTD present  no DTD 
DTD is read  Yes  No  Yes  Error 
entity substitution  Yes  No  Yes  Error 
defaulting of attributes  Yes  No  Yes  Error 
attribute normalization  Yes  No  Yes  Error 
checking against model  No  No  Yes  Error 

How do I associate my own data with a node in the DOM tree?
 

The class org.apache.xerces.dom.NodeImpl provides a void setUserData(Object o) and an Object getUserData() method that you can use to attach any object to a node in the DOM tree.

Beware that you should try and remove references to your data on nodes you no longer use (by calling setUserData(null), or these nodes will not be garbage collected until the entire document is garbage collected.


How do I more efficiently parse several documents sharing a common DTD?
 

DTDs are not currently cached by the parser. The common DTD, since it is specified in each XML document, will be re-parsed once for each document.

However, there are things that you can do now, to make the process of reading DTD's more efficient:

  • keep your DTD and DTD references local
  • use internal DTD subsets, if possible
  • load files from server to local client before parsing
  • Cache document files into a local client cache. You should do an HTTP header request to check whether the document has changed, before accessing it over the network.
  • Do not reference an external DTD or internal DTD subset at all. In this case, no DTD will be read.
  • Use a custom EntityResolver and keep common DTDs in a memory buffer.