http://xml.apache.org/http://www.apache.org/http://www.w3.org/

Home

Readme
Installation
Download
CVS Repository

Samples
API JavaDoc
FAQs

Features
Properties

XNI Manual
XML Schema
DOM
Limitations

Release Info
Report a Bug

Questions
 

Answers
 
Is Xerces DOM implementation thread-safe?
 

No. DOM does not require implementations to be thread safe. If you need to access the DOM from multiple threads, you are required to add the appropriate locks to your application code.


How do I supply my own implementation of the DOM?
 

Use http://apache.org/xml/properties/dom/document-class-name property to register your own implementation of the org.w3c.dom.Document interface.

Xerces provides the following implementations of the org.w3c.dom.Document interface:

  • org.apache.xerces.dom.CoreDocumentImpl -- supports DOM Level 2 Core Recommendation.
  • org.apache.xerces.dom.DocumentImpl -- supports DOM Level 2 Core, Mutation Events, Traversal and Ranges.
  • org.apache.xerces.dom.PSVIDocumentImpl -- provides access to the post schema validation infoset via DOM.

How do I access the DOM Level 3 functionality?
 

The DOM Level 3 functionality is not exposed in the regular Xerces distribution. To get access to the DOM Level 3, extract source code from CVS and build Xerces with the target jars-dom3. The build will generate the dom3-xmlParserAPIs.jar that includes the DOM Level 3 API and dom3-xercesImpl.jar that includes partial implementation of the API. The samples (i.e. samples.dom.DOM3) can be found in xercesSamples.jar.

For more information, refer to the DOM Level 3 Implementation page.

Note:Always remove build directory (either manually or by executing build clean target) before building specialized Xerces jars.

How do I create a DOM parser?
 

You can create a DOM parser by using the Java APIs for XML Processing (JAXP) or using the DOM Level 3 Load and Save.

The following source code shows how to create the parser with JAXP:

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-2_1_0/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
	}

The following source code shows how to create the parser using DOM Level 3:

	import  org.w3c.dom.DOMImplementationRegistry;
	import  org.w3c.dom.Document;
	import  org.w3c.dom.ls.DOMImplementationLS;
	import  org.w3c.dom.ls.DOMBuilder;

	...

	System.setProperty(DOMImplementationRegistry.PROPERTY,
	"org.apache.xerces.dom.DOMImplementationSourceImpl");
	DOMImplementationRegistry registry = 
	DOMImplementationRegistry.newInstance();

	DOMImplementationLS impl = 
	(DOMImplementationLS)registry.getDOMImplementation("LS-Load");

	DOMBuilder builder = impl.createDOMBuilder(
	DOMImplementationLS.MODE_SYNCHRONOUS, null);
	
	Document document = builder.parseURI("data/personal.xml");
      
Note:You can use DOM Level 3 Load/Save interfaces with the default Xerces distribution. To access the DOM Level 3 Core functionality you need to extract the code from CVS and build Xerces with the jars-dom3 target.

How do I serialize DOM to an output stream?
 

You can serialize a DOM tree by using Xerces org.apache.xml.XMLSerializer:

	import org.apache.xml.serialize.OutputFormat;
	import org.apache.xml.serialize.XMLSerializer;
	import org.apache.xml.serialize.LineSeparator;

	...

	OutputFormat format = new OutputFormat((Document)core);
	format.setLineSeparator(LineSeparator.Windows);
	format.setIndenting(true);
	format.setLineWidth(0);             
	format.setPreserveSpace(true);
	XMLSerializer serializer = new XMLSerializer (
	new FileWriter("output.xml"), format);
	serializer.asDOMSerializer();
	serializer.serialize(document);


      

You can also serialize a DOM tree by using the DOM Level 3 Load and Save. DOMWriter performs automatic namespace fixup to make your document namespace well-formed.

	import  org.w3c.dom.DOMImplementationRegistry;
	import  org.w3c.dom.Document;
	import  org.w3c.dom.ls.DOMImplementationLS;
	import  org.w3c.dom.ls.DOMWriter;

	...

	System.setProperty(DOMImplementationRegistry.PROPERTY,
	"org.apache.xerces.dom.DOMImplementationSourceImpl");
	DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();

	DOMImplementationLS impl = 
	(DOMImplementationLS)registry.getDOMImplementation("LS-Load");

	...     

	DOMWriter builder = impl.createDOMWriter();
	writer.writeNode(System.out, document);
      

How can I make sure that my DOM document in memory conforms to a schema?
 

DOM revalidation is supported via W3C DOM Level 3 Core Document.normalizeDocument().

Note:This release only supports revalidation against XML Schemas. Revalidation against DTDs or any other schema type is not implemented.

To revalidate the document you need:

  • Build DOM Level 3 Xerces jars (see How do I access the DOM Level 3 functionality).
  • Create the DOMBuilder (see Creating DOM parser) or use DOM methods to create a tree in memory.
  • Set validate feature using setNormalizationFeature method.
  • Make sure your document has xsi:schemaLocation or xsi:noSchemaLocation attributes at the document root that specify the location of schema(s) against which validation should occur.
  • The documentURI must be set. Locations of the schema documents will be resolved relative to the documentURI.
	import  org.w3c.dom.Document;
	import  org.w3c.dom.ls.DOMBuilder;

	.....
	Document document = builder.parseURI("data/personal.xml");
	document.setErrorHandler(new MyErrorHandler());
	document.setNormalizationFeature("validate", true);
	document.normalizeDocument();

	
      

For more information, please refer to the DOM Level 3 Implementation page.


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);

      

If you are using DOM Level 3 you can register an error handler with the DOMBuilder by supplying a class which implements the org.w3c.dom.DOMErrorHandler interface. Note: all exceptions during parsing or saving XML data are reported via DOMErrorHandler.


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

The Xerces http://apache.org/xml/features/dom/create-entity-ref-nodes feature (or corresponding DOM Level 3 DOMBuilder entities feature) controls 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.


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

The class org.apache.xerces.dom.NodeImpl provides the setUserData(Object o) and the Object getUserData() methods 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.

If you are using Xerces with the DOM Level 3 support you can use org.w3c.dom.Node.setUserData() and register your own UserDataHandler.




Copyright © 1999-2002 The Apache Software Foundation. All Rights Reserved.