<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE faqs SYSTEM 'dtd/faqs.dtd'>
<faqs title='Programming with DOM'>

  <faq title="Concurrent access">
    <q>Is Xerces DOM implementation thread-safe?</q>
    <a> <p>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. 
      </p>
    </a>
  </faq>
  <faq title="Specifying non-Xerces DOM implementation">
    <q>How do I supply my own implementation of the DOM?</q>
    <a>
      <p>
	Use 
	<jump href="">http://apache.org/xml/properties/dom/document-class-name</jump> property
	to register your own implementation of the 
        <code>org.w3c.dom.Document</code> interface.</p>
      <p>
        Xerces provides the following implementations of the 
	<code>org.w3c.dom.Document</code> interface:</p>
      <ul>
	<li><code>org.apache.xerces.dom.CoreDocumentImpl</code> --  
	  supports DOM Level 2 Core Recommendation.</li>
	<li><code>org.apache.xerces.dom.DocumentImpl</code> -- 
	  supports DOM Level 2 Core, Mutation Events, Traversal and Ranges.</li>
	<li><code>org.apache.xerces.dom.PSVIDocumentImpl</code> -- 
	  provides access to the post schema validation infoset via DOM.</li>
      </ul>
      
    </a>
  </faq>

  <faq title="Accessing the DOM Level 3 API">
    <q>How do I access the DOM Level 3 functionality?</q>
    <a>    <anchor name="dom3"/>
    <p>
	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 <strong>jars-dom3</strong>. The build will generate 
	the <code>dom3-xmlParserAPIs.jar</code> that includes the DOM Level 3 API and 
	<code>dom3-xercesImpl.jar</code> that includes partial implementation of the API. 
	The samples (i.e. <code>samples.dom.DOM3</code>) can be found in 
	<code>xercesSamples.jar</code>.
      </p> 
      <p>For more information, refer to the <link idref='dom3'>DOM Level 3 
	  Implementation</link> page. 
      </p>
      <note>Always remove build directory (either manually or by 
	executing build <code>clean</code> target)
	before building specialized Xerces jars.
      </note>
    </a>
  </faq>

  <faq title='Creating a DOM Parser'>
    <q>How do I create a DOM parser?</q>
    <a>
      <p>
	You can create a DOM parser by using  the Java APIs for
	XML Processing (JAXP) or using the DOM Level 3 Load and Save.
      </p>
      <p>  
	The following source code shows how to create the parser with JAXP:
      </p>
      <source>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 = &quot;file:///&parserdir;/data/personal.xml&quot;; 
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
}</source>
      <anchor name="domparser"/>
      <p>  
	The following source code shows how to create the parser using <jump href="http://www.w3.org/DOM/DOMTR#DOML3">DOM Level 3</jump>:
      </p>
      <source>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");</source>
      <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 <strong>jars-dom3</strong> target.</note>

    </a>
  </faq>

  <faq title="Serializing a DOM document.">
    <q>How do I serialize DOM to an output stream?</q>
    <a> <p>
	You can serialize a DOM tree by using Xerces <code>org.apache.xml.XMLSerializer</code>:
      </p>
      <source>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);</source>
      <p>You can also serialize a DOM tree by using the DOM Level 3 Load and Save. 
	<code>DOMWriter</code> performs automatic namespace fixup to make your document namespace
	well-formed.
      </p>
      <source>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);</source>  
    </a>
  </faq>


  <faq title="Revalidation of DOM document in Memory">
  	<q>How can I make sure that my DOM document in memory conforms to a schema?</q>
  	<a>
  		<p>
  			DOM revalidation is supported via W3C DOM Level 3 Core
  			<em>Document.normalizeDocument()</em>.
  			.
  		</p>
  		<note>This release only supports revalidation against XML Schemas. Revalidation against DTDs or any other schema type is not implemented.</note>

  		<p>To revalidate the document you need:</p>
  		<ul>
  			<li>
  				<jump href="#dom3">Build</jump> DOM Level 3 Xerces jars.  			
  		    </li>
  			<li>
  				<jump href="#domparser">Create</jump> the DOMBuilder.
  			</li>
  			<li>
  				Retrieve <code>DOMConfiguration</code> from the <code>Document</code>, 
  				and set <em>validate</em> feature to true.
  			</li>
  			<li>
  			   Provide XML Schemas (agains which validation should occur)
  			   by either setting <em>xsi:schemaLocation</em>/
  				<em>xsi:noSchemaLocation</em> attributes on the <code>documentElement</code>, or
  			   by setting <code>schema-location</code> parameter on the 
  			   <code>DOMConfiguration</code>.
  			</li>
  			<li>
  				Relative URIs for the schema documents will be resolved relative to the
  		       <jump href="http://www.w3.org/TR/2002/WD-DOM-Level-3-Core-20020409/core.html#Document3-documentURI">documentURI</jump>
  		       (which should be set).
  		       Otherwise, you can implement your own <code>DOMEntityResolver</code> and set it 
  		       via <code>entity-resolver</code> on the <code>DOMConfiguration</code>.
  			</li>
  		</ul>
  		<p>
  			<strong>Note:</strong> if a document contains any DOM Level 1 nodes (the nodes created using createElement, 
  			createAttribute, etc.) the fatal error will occur as described in the 
  			<jump href='http://www.w3.org/TR/2002/WD-DOM-Level-3-Core-20021022/namespaces-algorithms.html'>Namespace Normalization</jump>
  			algorithm.
  			In general, the
  			<jump href='http://www.w3.org/TR/2002/WD-DOM-Level-3-Core-20021022/core.html#Namespaces-Considerations'>DOM specification</jump>
  			discourages using DOM Level 1 nodes in the namespace aware application:
  		</p>
  		<p><em>DOM Level 1 methods are namespace ignorant. Therefore, while it is safe to use these methods when not 
  		dealing with namespaces, using them and the new ones at the same time should be avoided. DOM Level 1 methods 
  		solely identify attribute nodes by their nodeName. On the contrary, the DOM Level 2 methods related to namespaces, 
  		identify attribute nodes by their namespaceURI and localName. Because of this fundamental difference, mixing both 
  		sets of methods can lead to unpredictable results.</em></p>
 
  		<source>
import org.w3c.dom.Document;
import org.w3c.dom.DOMConfiguration;
import org.w3c.dom.ls.DOMBuilder;

..... 

Document document = builder.parseURI("data/personal-schema.xml");
DOMConfiguration config = document.getConfig();
config.setParameter("error-handler",new MyErrorHandler());
config.setParameter("validate", Boolean.TRUE);
document.normalizeDocument();</source>

  		<p>
  			For more information, please refer to the
  			<link idref='dom3'>DOM Level 3 Implementation</link>
  			page.
  		</p>
  	</a>
  </faq>

  <faq title='Handling Errors in DOM'>
    <q>How do handle errors?</q>
    <a>
      <p>
	You should register an error handler with the parser by supplying
	a class which implements the <code>org.xml.sax.ErrorHandler</code>
	interface. This is true regardless of whether your parser is a
	DOM based or SAX based parser.
      </p>
      <p>
	You can register an error handler on a <code>DocumentBuilder</code>
	created using JAXP like this:
      </p>
      <source>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);</source>
      <p>If you are using <jump href="http://www.w3.org/DOM/DOMTR#DOML3">DOM Level 3</jump> 
	you can register an error handler with the <code>DOMBuilder</code> by supplying
	a class which implements the <code>org.w3c.dom.DOMErrorHandler</code>
	interface. <strong>Note:</strong> all exceptions during parsing or saving XML data 
	are reported via DOMErrorHandler.</p>
    </a>
  </faq>


  <faq title='Controlling Entity Representation'>
    <q>
      How can I control the way that entities are represented in the DOM?
    </q>
    <a>
      <p>The Xerces  <code>http://apache.org/xml/features/dom/create-entity-ref-nodes</code> 
        feature
	(or corresponding DOM Level 3 DOMBuilder <code>entities</code> 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.
      </p>
      <p>
	If the feature is false, an entity reference in the XML document
	is represented by only the nodes that represent the entity 
	expansion.
      </p> 
      <p>
	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.
      </p>
    </a>
  </faq>


  <faq title='Associating user data with a Node'>
    <q>How do I associate my own data with a node in the DOM tree?</q>
    <a>
      <p>
	The class <code>org.apache.xerces.dom.NodeImpl</code> provides the 
	<code>setUserData(Object o)</code> and the <code>Object 
	  getUserData()</code> methods that you can use to attach any object
	to a node in the DOM tree.
      </p>
      <p>
	Beware that you should try and remove references to your data on
	nodes you no longer use (by calling <code>setUserData(null)</code>,
	or these nodes will not be garbage collected until the entire
	document is garbage collected.
      </p>
      <p>If you are using Xerces with the DOM Level 3 support  
	you can use <code>org.w3c.dom.Node.setUserData()</code> and register your own 
	<code>UserDataHandler</code>. </p>
    </a>
  </faq>

<faq title='Making getElementById() work'>
  <q>Why does not getElementById()  work for documents validated against XML Schemas?</q>
  <a>
  <p>Make sure the <link idref='features' anchor="validation">validation feature</link> and the 
<link idref='features' anchor="validation.schema">schema feature</link> are turned on before you parse a document.
   
  </p>
  </a>
 </faq>
 
 
   <faq title="Setting ID attribute">
    <q>How do I specify an ID attribute in the DOM?</q>
    <a> <p>You can use the DOM level 3 setIdAttribute, setIdAttributeNS, and setIdAttributeNode methods
    to specify ID attribute in the DOM. See <jump href="#dom3">DOM Level 3</jump>.
      </p>
    </a>
  </faq>
  
     <faq title="Accessing type information">
    <q>How do I access type information in the DOM?</q>
    <a> <p><jump href="#dom3">DOM Level 3</jump> defines a  <jump href="http://www.w3.org/TR/2002/WD-DOM-Level-3-Core-20021022/core.html#TypeInfo">TypeInfo</jump> 
    interface that exposes type information for 
     element and attribute nodes. The type information depends on the document schema and is only available
     if Xerces was able to find the corresponding grammar (DOM Level 3 <code>validate</code> or 
     <code>validate-if-schema</code> feature must be turned on).  
     If you need to access the full PSVI in the DOM please refer to <jump href="faq-xs.html#faq-5">Using XML Schemas</jump>.
      </p>
    </a>
  </faq>
</faqs>
