~ubuntu-branches/ubuntu/karmic/libxerces2-java/karmic

« back to all changes in this revision

Viewing changes to docs/faq-dom.xml

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2006-12-04 17:37:55 UTC
  • mfrom: (2.1.2 etch)
  • Revision ID: james.westby@ubuntu.com-20061204173755-hb6ybrrrk097zhx7
Tags: 2.8.1-1ubuntu1
* Merge with Debian unstable; remaining changes:
  - Build -gcj package.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
<?xml version='1.0' encoding='UTF-8'?>
 
2
<!--
 
3
 * Copyright 2002-2006 The Apache Software Foundation.
 
4
 *
 
5
 * Licensed under the Apache License, Version 2.0 (the "License");
 
6
 * you may not use this file except in compliance with the License.
 
7
 * You may obtain a copy of the License at
 
8
 *
 
9
 *     http://www.apache.org/licenses/LICENSE-2.0
 
10
 *
 
11
 * Unless required by applicable law or agreed to in writing, software
 
12
 * distributed under the License is distributed on an "AS IS" BASIS,
 
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
14
 * See the License for the specific language governing permissions and
 
15
 * limitations under the License.
 
16
-->
2
17
<!DOCTYPE faqs SYSTEM 'dtd/faqs.dtd'>
3
18
<faqs title='Programming with DOM'>
4
19
 
66
81
    DOMImplementationRegistry.newInstance();
67
82
 
68
83
DOMImplementationLS impl = 
69
 
    (DOMImplementationLS)registry.getDOMImplementation("LS-Load");
 
84
    (DOMImplementationLS)registry.getDOMImplementation("LS");
70
85
 
71
86
LSParser builder = impl.createLSParser(
72
87
    DOMImplementationLS.MODE_SYNCHRONOUS, null);
73
88
        
74
89
Document document = builder.parseURI("data/personal.xml");</source>
75
 
      <note>You can use DOM Level 3 Load/Save interfaces with the default Xerces distribution. 
76
 
        To access the DOM Level 3 Core functionality you need to extract the code from 
77
 
        CVS and build Xerces with the <strong>jars-dom3</strong> target.</note>
78
 
 
 
90
      <note>You can now use DOM Level 3 Load/Save and Core interfaces with the default Xerces distribution. 
 
91
      </note>
79
92
    </a>
80
93
  </faq>
81
94
 
82
95
  <faq title="Serializing a DOM document">
83
96
    <q>How do I serialize DOM to an output stream?</q>
84
97
    <a><anchor name="dom-xml-serialization"/>
85
 
      <p>
86
 
        You can serialize a DOM tree by using Xerces <code>org.apache.xml.XMLSerializer</code>:
 
98
      <p>You can serialize a DOM tree by using the DOM Level 3 Load and Save. 
 
99
        <code>LSSerializer</code> performs automatic namespace fixup to make your document namespace
 
100
        well-formed.
 
101
      </p>
 
102
      <source>import  org.w3c.dom.DOMImplementationRegistry;
 
103
import  org.w3c.dom.Document;
 
104
import  org.w3c.dom.ls.DOMImplementationLS;
 
105
import  org.w3c.dom.ls.LSSerializer;
 
106
 
 
107
...
 
108
 
 
109
System.setProperty(DOMImplementationRegistry.PROPERTY,
 
110
    "org.apache.xerces.dom.DOMImplementationSourceImpl");
 
111
DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
 
112
 
 
113
DOMImplementationLS impl = 
 
114
    (DOMImplementationLS)registry.getDOMImplementation("LS");
 
115
 
 
116
...     
 
117
 
 
118
LSSerializer writer = impl.createLSSerializer();
 
119
String str = writer.writeToString(document);</source>  
 
120
 
 
121
      <p>
 
122
        You can also serialize a DOM tree by using the JAXP Transformer API.
 
123
      </p>
 
124
 
 
125
      <p>
 
126
        You can also serialize a DOM tree by using the Xerces <code>org.apache.xml.XMLSerializer</code> serialization code directly.  This is a non-standard way of serializing a DOM and should be avoided:
87
127
      </p>
88
128
      <source>import org.apache.xml.serialize.OutputFormat;
89
129
import org.apache.xml.serialize.XMLSerializer;
100
140
    new FileWriter("output.xml"), format);
101
141
serializer.asDOMSerializer();
102
142
serializer.serialize(document);</source>
103
 
      <p>You can also serialize a DOM tree by using the DOM Level 3 Load and Save. 
104
 
        <code>DOMWriter</code> performs automatic namespace fixup to make your document namespace
105
 
        well-formed.
106
 
      </p>
107
 
      <source>import  org.w3c.dom.DOMImplementationRegistry;
108
 
import  org.w3c.dom.Document;
109
 
import  org.w3c.dom.ls.DOMImplementationLS;
110
 
import  org.w3c.dom.ls.LSWriter;
111
 
 
112
 
...
113
 
 
114
 
System.setProperty(DOMImplementationRegistry.PROPERTY,
115
 
    "org.apache.xerces.dom.DOMImplementationSourceImpl");
116
 
DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
117
 
 
118
 
DOMImplementationLS impl = 
119
 
    (DOMImplementationLS)registry.getDOMImplementation("LS-Load");
120
 
 
121
 
...     
122
 
 
123
 
LSWriter writer = impl.createLSSerializer();
124
 
String str = writer.writeToString(document);</source>  
125
143
    </a>
126
144
  </faq>
127
145
  
157
175
        <code>org.w3c.dom.Document</code> interface:</p>
158
176
      <ul>
159
177
        <li><code>org.apache.xerces.dom.CoreDocumentImpl</code> --  
160
 
          supports DOM Level 2 Core Recommendation.</li>
 
178
          supports DOM Level 3 Core Recommendation.</li>
161
179
        <li><code>org.apache.xerces.dom.DocumentImpl</code> -- 
162
 
          supports DOM Level 2 Core, Mutation Events, Traversal and Ranges.</li>
 
180
          supports DOM Level 3 Core, Mutation Events, Traversal and Ranges.</li>
163
181
        <li><code>org.apache.xerces.dom.PSVIDocumentImpl</code> -- 
164
182
          provides access to the post schema validation infoset via DOM.</li>
165
183
      </ul>
171
189
  <faq title="Accessing the DOM Level 3 API">
172
190
    <q>How do I access the DOM Level 3 functionality?</q>
173
191
    <a>    <anchor name="dom3"/>
174
 
    <p>
175
 
        The DOM Level 3 functionality is not exposed in the regular 
176
 
        Xerces distribution.
177
 
        To get access to the DOM Level 3, you need either to extract source code from CVS or 
178
 
        to download both Xerces source and tools distributions and 
179
 
        build Xerces with the target <strong>jars-dom3</strong>. The build will generate 
180
 
        the <code>dom3-xml-apis.jar</code> that includes the DOM Level 3 API and 
181
 
        <code>dom3-xercesImpl.jar</code> that includes partial implementation of the API. 
182
 
        The samples (i.e. <code>samples.dom.DOM3</code>) can be found in 
183
 
        <code>xercesSamples.jar</code>.
184
 
      </p> 
185
 
      <p>For more information, refer to the <link idref='dom3'>DOM Level 3 
186
 
          Implementation</link> page. 
187
 
      </p>
188
 
      <note>Always remove build directory (either manually or by 
189
 
        executing build <code>clean</code> target)
190
 
        before building specialized Xerces jars.
191
 
      </note>
 
192
     <p>
 
193
      The DOM Level 3 functionality is now exposed by default since Xerces-J 2.7.0.
 
194
     </p>
 
195
     <p>
 
196
      The experimental interfaces which were once present in the <code>org.apache.xerces.dom3</code>
 
197
      package no longer exist. This package existed primarily so that the DOM Level 2
 
198
      and DOM Level 3 implementations in Xerces-J 2.6.2 and prior could co-exist. Code which
 
199
      depended on the <code>org.apache.xerces.dom3</code> package must be modified to use the 
 
200
      official DOM Level 3 API located in the <code>org.w3c.dom.*</code> packages.
 
201
     </p> 
 
202
     <p>For more information, refer to the <link idref='dom3'>DOM Level 3 
 
203
        Implementation</link> page.
 
204
     </p>    
192
205
    </a>
193
206
  </faq>
194
207
  
196
209
    <q>How do I access run under Sun JDK 1.4 and higher?</q>
197
210
    <a>    <anchor name="dom3-run"/>
198
211
    <p>Use the <jump href="http://java.sun.com/j2se/1.4/docs/guide/standards/">
199
 
    Endorsed Standards Override Mechanism</jump> to specify dom3-xml-apis.jar.
200
 
    More complete description is available <jump href="http://xml.apache.org/xalan-j/faq.html#faq-N100CB">here</jump>.
 
212
    Endorsed Standards Override Mechanism</jump> to specify xercesImpl.jar and xml-apis.jar.
 
213
    More complete description is available <jump href="http://xml.apache.org/xalan-j/faq.html#faq-N100CC">here</jump>.
201
214
    </p>
202
215
    </a>
203
216
  </faq>
208
221
<a> <anchor name="xsdom"/>
209
222
<p>By default Xerces does not store the PSVI information in the DOM tree. </p>
210
223
<p>
211
 
The following source shows you how to parse an XML document (using JAXP) and how to retrieve PSVI (using the <jump href="http://www.w3.org/Submission/2004/SUBM-xmlschema-api-20040122/">XML Schema API</jump>):
 
224
The following source shows you how to parse an XML document (using JAXP) and how to retrieve PSVI (using the <jump href="http://www.w3.org/Submission/xmlschema-api/">XML Schema API</jump>):
212
225
<source>
213
226
//<code>dbf</code> is JAXP DocumentBuilderFactory
214
227
 
242
255
    (DOMImplementation) registry.getDOMImplementation("psvi");
243
256
</source>
244
257
 
245
 
The PSVI information will not be added or modified as you modify the tree in memory. Instead, if you want to get updated PSVI information, you need to validate your DOM in memory using the <jump href="http://www.w3.org/TR/2003/CR-DOM-Level-3-Core-20031107/core.html#Document3-normalizeDocument">normalizeDocument</jump> method as described in the <jump href="#xsrevalidate">next question</jump>.
 
258
The PSVI information will not be added or modified as you modify the tree in memory. Instead, if you want to get updated PSVI information, you need to validate your DOM in memory using the <jump href="http://www.w3.org/TR/DOM-Level-3-Core/core.html#Document3-normalizeDocument">normalizeDocument</jump> method as described in the <jump href="#xsrevalidate">next question</jump>.
246
259
 
247
260
<p>You can find more information about how to use the XML Schema API <jump href="faq-xs.html">here</jump>.</p>
248
261
</a>
256
269
                        DOM revalidation is supported via W3C DOM Level 3 Core
257
270
                        <em>Document.normalizeDocument()</em>.
258
271
                </p>
259
 
                <note>This release only supports revalidation against XML Schemas. Revalidation against DTDs or any other schema type is not implemented.</note>
 
272
                <note>This release only supports revalidation against XML Schemas and DTDs. Revalidation against other schema types is not implemented.</note>
260
273
 
261
274
                <p>To revalidate the document you need:</p>
262
275
                <ul>
263
276
                        <li>
264
 
                                <jump href="#dom3">Build</jump> DOM Level 3 Xerces jars.                        
265
 
                    </li>
266
 
                        <li>
267
277
                                <jump href="#domparser">Create</jump> the DOMParser.
268
278
                        </li>
269
279
                        <li>
279
289
                        </li>
280
290
                        <li>
281
291
                                Relative URIs for the schema documents will be resolved relative to the
282
 
                       <jump href="http://www.w3.org/TR/2003/CR-DOM-Level-3-Core-20031107/core.html#Document3-documentURI">documentURI</jump>
 
292
                       <jump href="http://www.w3.org/TR/DOM-Level-3-Core/core.html#Document3-documentURI">documentURI</jump>
283
293
                       (which should be set).
284
 
                       Otherwise, you can implement your own <code>DOMResourceResolver</code> and set it 
 
294
                       Otherwise, you can implement your own <code>LSResourceResolver</code> and set it 
285
295
                       via <code>resource-resolver</code> on the <code>DOMConfiguration</code>.
286
296
                        </li>
287
297
                </ul>
288
298
                <p>
289
299
                        <strong>Note:</strong> if a document contains any DOM Level 1 nodes (the nodes created using createElement, 
290
300
                        createAttribute, etc.) a fatal error will occur as described in the 
291
 
                        <jump href='http://www.w3.org/TR/2003/CR-DOM-Level-3-Core-20031107/namespaces-algorithms.html'>Namespace Normalization</jump>
 
301
                        <jump href='http://www.w3.org/TR/DOM-Level-3-Core/namespaces-algorithms.html'>Namespace Normalization</jump>
292
302
                        algorithm.
293
303
                        In general, the
294
 
                        <jump href='http://www.w3.org/TR/2003/CR-DOM-Level-3-Core-20031107/core.html#Namespaces-Considerations'>DOM specification</jump>
 
304
                        <jump href='http://www.w3.org/TR/DOM-Level-3-Core/core.html#Namespaces-Considerations'>DOM specification</jump>
295
305
                        discourages using DOM Level 1 nodes in the namespace aware application:
296
306
                </p>
297
307
                <p><em>DOM Level 1 methods are namespace ignorant. Therefore, while it is safe to use these methods when not 
308
318
..... 
309
319
 
310
320
Document document = builder.parseURI("data/personal-schema.xml");
311
 
DOMConfiguration config = document.getConfig();
 
321
DOMConfiguration config = document.getDomConfig();
312
322
config.setParameter("error-handler",new MyErrorHandler());
 
323
config.setParameter("schema-type", "http://www.w3.org/2001/XMLSchema");
313
324
config.setParameter("validate", Boolean.TRUE);
314
325
document.normalizeDocument();</source>
315
326
 
355
366
DocumentBuilder builder = /* builder instance */;
356
367
builder.setErrorHandler(handler);</source>
357
368
      <p>If you are using <jump href="http://www.w3.org/DOM/DOMTR#DOML3">DOM Level 3</jump> 
358
 
        you can register an error handler with the <code>DOMBuilder</code> by supplying
 
369
        you can register an error handler with the <code>LSParser</code> by supplying
359
370
        a class which implements the <code>org.w3c.dom.DOMErrorHandler</code>
360
371
        interface. <strong>Note:</strong> all exceptions during parsing or saving XML data 
361
372
        are reported via DOMErrorHandler.</p>
368
379
    <a>
369
380
      <p>The Xerces  <code>http://apache.org/xml/features/dom/create-entity-ref-nodes</code> 
370
381
        feature
371
 
        (or corresponding DOM Level 3 DOMBuilder <code>entities</code> feature)  
 
382
        (or corresponding DOM Level 3 LSParser <code>entities</code> feature)  
372
383
        controls how entities appear in the DOM tree. When one of those features 
373
384
        is set to true (the default), an occurrence of an entity reference
374
385
        in the XML document will be represented by a subtree with an 
431
442
  
432
443
     <faq title="Accessing type information">
433
444
    <q>How do I access type information in the DOM?</q>
434
 
    <a> <p><jump href="#dom3">DOM Level 3</jump> defines a  <jump href="http://www.w3.org/TR/2003/CR-DOM-Level-3-Core-20031107/core.html#TypeInfo">TypeInfo</jump> 
 
445
    <a> <p><jump href="#dom3">DOM Level 3</jump> defines a  <jump href="http://www.w3.org/TR/DOM-Level-3-Core/core.html#TypeInfo">TypeInfo</jump> 
435
446
    interface that exposes type information for 
436
447
     element and attribute nodes. The type information depends on the document schema and is only available
437
448
     if Xerces was able to find the corresponding grammar (DOM Level 3 <code>validate</code> or