/usr/share/doc/libdom4j-java-doc/faq.html is in libdom4j-java-doc 1.6.1+dfsg.3-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html><head><title>dom4j - Frequently Asked Questions</title><style type="text/css" media="all">
@import url("./style/maven-base.css");
@import url("./style/maven-theme.css");</style><link rel="stylesheet" href="./style/print.css" type="text/css" media="print"></link><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"></meta></head><body class="composite"><div id="banner"><a href="http://sourceforge.net/projects/dom4j" id="organizationLogo"><img alt="MetaStuff Ltd." src="http://sourceforge.net/sflogo.php?group_id=16035"></img></a><a href="http://dom4j.org" id="projectLogo"><img alt="dom4j" src="./images/logo.gif"></img></a><div class="clear"><hr></hr></div></div><div id="breadcrumbs"><div class="xleft">
Last published: 16 May 2005
<span class="separator">|</span>
Doc for 1.6.1
</div><div class="xright">
<a href="http://www.w3.org/TR/xpath" class="externalLink" title="External Link">XPath Spec</a>
<span class="separator">|</span>
<a href="http://jaxen.codehaus.org/" class="externalLink" title="External Link">Jaxen</a>
<span class="separator">|</span>
<a href="http://sourceforge.net/projects/dom4j/" class="externalLink" title="External Link">dom4j@SourceForge</a>
</div><div class="clear"><hr></hr></div></div><div id="leftColumn"><div id="navcolumn"><div id="menudom4j"><h5>dom4j</h5><ul><li class="none"><a href="index.html">Home</a></li><li class="none"><strong><a href="faq.html">FAQ</a></strong></li><li class="none"><a href="guide.html">Quick start</a></li><li class="none"><a href="cookbook.html" class="newWindow" title="New Window" target="_blank">Cookbook</a></li><li class="none"><a href="compare.html">Comparison</a></li><li class="none"><a href="goals.html">Goals</a></li><li class="none"><a href="changes-report.html">Changes</a></li><li class="none"><a href="download.html">Download</a></li><li class="none"><a href="license.html">License</a></li><li class="none"><a href="apidocs/index.html" class="newWindow" title="New Window" target="_blank">Javadoc (1.6.1)</a></li><li class="none"><a href="http://www.dom4j.org/dom4j-1.5.2/apidocs/index.html" class="newWindow" title="New Window" target="_blank">Javadoc (1.5.2)</a></li><li class="none"><a href="http://www.dom4j.org/dom4j-1.4/apidocs/index.html" class="newWindow" title="New Window" target="_blank">Javadoc (1.4)</a></li></ul></div><div id="menuProject_Documentation"><h5>Project Documentation</h5><ul><li class="none"><a href="index.html">About dom4j</a></li><li class="collapsed"><a href="project-info.html">Project Info</a></li><li class="expanded"><a href="maven-reports.html">Project Reports</a><ul><li class="none"><a href="changelog-report.html">Change Log</a></li><li class="none"><a href="changes-report.html">Changes</a></li><li class="none"><a href="checkstyle-report.html">Checkstyle</a></li><li class="none"><a href="clover/index.html" class="newWindow" title="New Window" target="_blank">Clover</a></li><li class="none"><a href="developer-activity-report.html">Developer Activity</a></li><li class="none"><strong><a href="faq.html">FAQs</a></strong></li><li class="none"><a href="file-activity-report.html">File Activity</a></li><li class="none"><a href="apidocs/index.html" class="newWindow" title="New Window" target="_blank">JavaDocs</a></li><li class="none"><a href="javadoc.html">JavaDoc Report</a></li><li class="none"><a href="javadoc-warnings-report.html">JavaDoc Warnings Report</a></li><li class="none"><a href="jdepend-report.html">Metrics</a></li><li class="none"><a href="junit-report.html">Unit Tests</a></li><li class="none"><a href="xref/index.html" class="newWindow" title="New Window" target="_blank">Source Xref</a></li><li class="none"><a href="xref-test/index.html" class="newWindow" title="New Window" target="_blank">Test Xref</a></li><li class="none"><a href="license.html">Project License</a></li></ul></li><li class="none"><a href="http://maven.apache.org/development-process.html" class="externalLink" title="External Link">Development Process</a></li></ul></div><div id="legend"><h5>Legend</h5><ul><li class="externalLink">External Link</li><li class="newWindow">Opens in a new window</li></ul></div><a href="http://www.cenqua.com/clover" title="Code Coverage by Clover" id="poweredByExternal"><img alt="Code Coverage by Clover" src="http://www.cenqua.com/images/clovered1.gif"></img></a><a href="http://www.sourceforge.net" title="Hosted by SourceForge" id="poweredByExternal"><img alt="Hosted by SourceForge" src="http://sourceforge.net/sflogo.php?group_id=16035&amp;type=1"></img></a><a href="http://maven.apache.org/" title="Built by Maven" id="poweredBy"><img alt="Built by Maven" src="./images/logos/maven-button-1.png"></img></a></div></div><div id="bodyColumn"><div class="contentBox"><div class="section"><a name="Frequently_Asked_Questions"></a><h2>Frequently Asked Questions</h2><p>
<strong>General</strong>
</p><ol>
<li>
<a href="#whats-dom4j">
What is dom4j?
</a>
</li>
<li>
<a href="#whats-license">
What is the dom4j license?
</a>
</li>
<li>
<a href="#classpath">
What do I need to add to my CLASSPATH?
</a>
</li>
<li>
<a href="#whats-dom4j-v-dom">
How does dom4j relate to DOM?
</a>
</li>
<li>
<a href="#whats-dom4j-v-jdom">
How does dom4j relate to JDOM?
</a>
</li>
<li>
<a href="#dom4j-sax">
How does dom4j work with DOM and SAX?
</a>
</li>
</ol><p>
<strong>Using dom4j</strong>
</p><ol>
<li>
<a href="#dom4j-xslt">
How can I use XSLT with dom4j?
</a>
</li>
<li>
<a href="#pretty-print">
How can I pretty print my XML document?
</a>
</li>
<li>
<a href="#from-string">
How can I parse a document from a String?
</a>
</li>
<li>
<a href="#compare-nodes">
How do I compare 2 nodes for equality?
</a>
</li>
<li>
<a href="#large-doc">
How does dom4j handle very large XML documents?
</a>
</li>
<li>
<a href="#visitor">
Does dom4j support the Visitor Pattern?
</a>
</li>
<li>
<a href="#sort-select">
Can I sort the List returned by Node.selectNodes()?
</a>
</li>
<li>
<a href="#whats-optional">
What features are optional in dom4j?
</a>
</li>
<li>
<a href="#why-jaxp-warning">
What does the following mean 'Warning: Error occurred using JAXP to load a SAXParser. Will use Aelfred instead'
</a>
</li>
<li>
<a href="#what-parser">
What XML parser does dom4j use?
</a>
</li>
<li>
<a href="#how-validate">
How can I validate my document?
</a>
</li>
<li>
<a href="#import-vaj">
How do I import dom4j into VAJ?
</a>
</li>
<li>
<a href="#cannot-find-dtd">
Cannot find DTD; how can I tell dom4j where to find the DTD from a DOCTYPE?
</a>
</li>
</ol></div><div class="section"><a name="General"></a><h2>General</h2><title>General</title><dl>
<dt><a name="whats-dom4j">What is dom4j?</a></dt>
<dd>
<p><i>dom4j</i> is an Open Source XML framework for Java.
<i>dom4j</i> allows you to read, write, navigate, create and modify
XML documents.
<i>dom4j</i> integrates with DOM and SAX and is seamlessly integrated
with full XPath support.
</p>
</dd>
</dl><dl>
<dt><a name="whats-license">What is the dom4j license?</a></dt>
<dd>
<p>We use an Apache-style open source license which is one of the least
restrictive licenses around, you can use <i>dom4j</i> to create new products
without them having to be open source.
</p>
<p>You can find a copy of the license <a href="license.html">here</a>.
</p>
</dd>
</dl><dl>
<dt><a name="classpath">What do I need to add to my CLASSPATH?</a></dt>
<dd>
<p>The dom4j.jar only contains the dom4j classes. If you want to use a SAX parser,
you'll have to include the SAX classes and the SAX parser of your choice to your
CLASSPATH. If you want to use XPath expressions, you also have to include the
jaxen.jar to your CLASSPATH.
</p>
<p><i>dom4j</i> can use your existing XML parser and/or DOM implementation
(such as <a href="http://xml.apache.org/crimson/" class="externalLink" title="External Link">Crimson</a> or
<a href="http://xml.apache.org/xerces-j/" class="externalLink" title="External Link">Xerces</a> if you want it to. <i>dom4j</i> can also use
<a href="http://java.sun.com/xml/" class="externalLink" title="External Link">JAXP</a> to configure which SAX Parser to use - just add the
jaxp.jar to your CLASSPATH and whichever SAX parser you wish away you go.
</p>
</dd>
</dl><dl>
<dt><a name="whats-dom4j-v-dom">How does dom4j relate to DOM?</a></dt>
<dd>
<p>DOM is a quite large language independent API.
<i>dom4j</i> is a simpler, lightweight API making extensive use of standard Java APIs such as the
Java 2 collections API.
</p>
<p>Remark that <i>dom4j</i> fully supports the DOM standard allowing both APIs to be
used easily together.
</p>
</dd>
</dl><dl>
<dt><a name="whats-dom4j-v-jdom">How does dom4j relate to JDOM?</a></dt>
<dd>
<p><i>dom4j</i> is a different project and different API to JDOM
though they both have similar goals. They both attempt to make it easier
to use XML on the Java platform.
They differ in their design, API and implementation.
</p>
<p><i>dom4j</i> is based on Java interfaces so that plug and play
document object model implementations are allowed and encouraged
such as small, read only, quick to create implementations or
bigger, highly indexed fast to naviagte implementations or
implementations which read themselves lazily from a database or
Java Beans etc.
</p>
<p><i>dom4j</i> uses polymorphism extensively such that all document object types
implement the Node interface.
Also both the Element and Document interfaces can be used polymorphically
as they both extend the Branch interface.
</p>
<p><i>dom4j</i> is fully integrated with XPath support throughout the API
so doing XPath expressions is as easy as
</p>
<pre>
SAXReader reader = new SAXReader();
Document document = reader.read( url );
List links = document.selectNodes( "//a[@href]" );
String title = document.valueOf( "/head/title" );
</pre>
<p><i>dom4j</i> will soon provide a configuration option to
support the W3C DOM API natively to avoid unnecessary tree duplication
when using <i>dom4j</i> with XSLT engines etc.
</p>
</dd>
</dl><dl>
<dt><a name="dom4j-sax">How does dom4j work with DOM and SAX?</a></dt>
<dd>
<p>You can create <i>dom4j</i> documents from XML text, SAX events or existing
DOM trees or you can write <i>dom4j</i> documents as SAX events, DOM trees
or XML text.
</p>
</dd>
</dl></div><div class="section"><a name="Using_dom4j"></a><h2>Using dom4j</h2><title>Using dom4j</title><dl>
<dt><a name="dom4j-xslt">How can I use XSLT with dom4j?</a></dt>
<dd>
<p><i>dom4j</i> integrates with XSLT using the JAXP standard (TrAX) APIs.
A <i>dom4j</i> Document can be used as the source of XML to be styled or
the source of the stylesheet.
A <i>dom4j</i> Document can also be used as the result of a transformation.
</p>
<p>First you'll need to use JAXP to load a Transformer.
</p>
<pre>
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamSource;
import org.dom4j.Document;
import org.dom4j.DocumentResult;
import org.dom4j.DocumentSource;
...
TransformerFactory factory
= TransformerFactory.newInstance();
Transformer transformer
= factory.newTransformer( new StreamSource( "foo.xsl" ) );
</pre>
<p>Now that you have a transformer its easy to style a Document into
a new Document.
</p>
<pre>
DocumentSource source = new DocumentSource( document );
DocumentResult result = new DocumentResult();
transformer.transform( source, result );
Document transformedDoc = result.getDocument();
</pre>
<p>If you want to transform a Document into XML text you can use JAXP as follows:-
</p>
<pre>
DocumentSource source = new DocumentSource( document );
DocumentResult result = new StreamResult( new FileReader( "output.xml" ) );
transformer.transform( source, result );
</pre>
<p>For more information on JAXP and (TrAX) try
<a href="http://java.sun.com/xml/" class="externalLink" title="External Link">Sun's JAXP site</a>.
</p>
</dd>
</dl><dl>
<dt><a name="pretty-print">How can I pretty print my XML document?</a></dt>
<dd>
<p>You can control the format of the XML text output by
<a href="apidocs/org/dom4j/io/XMLWriter.html">XMLWriter</a>
by using the
<a href="apidocs/org/dom4j/io/OutputFormat.html">OutputFormat</a>
object.
You can explicitly set the various formatting options via the
properties methods of the OutputFormat object. There is also a helper
method <code>OutputFormat.createPrettyPrint()</code> which creates
the default pretty-print format.
</p>
<p>So to pretty print some XML (trimming all whitespace and indenting nicely)
the following code should do the job...
</p>
<pre>
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter writer = new XMLWriter( out, format );
writer.write( document );
writer.close();
</pre>
</dd>
</dl><dl>
<dt><a name="from-string">How can I parse a document from a String?</a></dt>
<dd>
<p>Sometimes you have a String (or StringBuffer) which contains the XML
to be parsed. This can be parsed using <code>SAXReader</code>
and the <code>StringReader</code> from the JDK. For example:-
</p>
<pre>
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
public class Foo {
public Document getDocument() throws DocumentException {
return DocumentHelper.parseText(
"<root> <child id='1'>James</child> </root>"
);
}
}
</pre>
</dd>
</dl><dl>
<dt><a name="compare-nodes">How do I compare 2 nodes for equality?</a></dt>
<dd>
<p><i>dom4j</i> by default uses identity based equality for performance.
It avoids having to walk entire documents or document fragments when
putting nodes in collections.
</p>
<p>To compare 2 nodes (attributes, elements, documents etc) for equality
the <a href="apidocs/org/dom4j/util/NodeComparator.html">NodeComparator</a> can be
used.
</p>
<pre>
Node node1 = ...;
Node node2 = ...;
NodeComparator comparator = new NodeComparator();
if ( comparator.compare( node1, node2 ) == 0 ) {
// nodes are equal!
}
</pre>
<p>If you are having problems comparing documents that you think
are equal but the NodeComparator decides that they are different,
you might find the following useful.
</p>
<p>In dom4j/test/src/org/dom4j/AbstractTestCase.java is-a JUnit TestCase and is
an abstract base class for dom4j test cases. It contains a whole bunch of
useful assertion helper methods for testing documents, nodes and fragments
being equal. The nice thing is that you get useful messages telling you
exactly why they are different, so its pretty easy to track down.
For example.
</p>
<pre>
public MyTest extends AbstractTestCase {
...
public void testSomething() {
Document doc1 = ...;
Document doc2 = ...;
assertDocumentsEqual( doc1, doc2 );
...
assertNodesEqual( doc1.getRootElement(), doc2.getRootElement() );
}
}
</pre>
</dd>
</dl><dl>
<dt><a name="large-doc">How does dom4j handle very large XML documents?</a></dt>
<dd>
<p><i>dom4j</i> provides an event based model for processing XML documents.
Using this event based model allows developers to prune the XML tree
when parts of the document have been successfully processed avoiding
having to keep the entire document in memory.
</p>
<p>For example, imagine you need to process a very large XML file that is
generated externally by some database process and looks something like
the following (where N is a very large number).
</p>
<pre>
<ROWSET>
<ROW id="1">
...
</ROW>
<ROW id="2">
...
</ROW>
...
<ROW id="N">
...
</ROW>
</ROWSET>
</pre>
<p>We can process each <ROW> at a time, there is no
need to keep all of them in memory at once.
<i>dom4j</i> provides a <i>Event Based Mode</i> for this purpose.
We can register an event handler for one or more path expressions.
These handlers will then be called on the start and end of each path
registered against a particular handler. When the start tag of a
path is found, the onStart method of the handler registered to the
path is called. When the end tag of a path if found, the onEnd method
of the handler registered to that path is called.
</p>
<p>
The onStart and onEnd methods are passed an instance of an ElementPath,
which can be used to retrieve the current Element for the given path.
If the handler wishes to "prune" the tree being built in order to
save memory use, it can simply call the detach() method of the current
Element being processed in the handlers onEnd() method.
</p>
<p>
So to process each <ROW> individually we can do the following.
</p>
<pre>
// enable pruning mode to call me back as each ROW is complete
SAXReader reader = new SAXReader();
reader.addHandler( "/ROWSET/ROW",
new ElementHandler() {
public void onStart(ElementPath path) {
// do nothing here...
}
public void onEnd(ElementPath path) {
// process a ROW element
Element row = path.getCurrent();
Element rowSet = row.getParent();
Document document = row.getDocument();
...
// prune the tree
row.detach();
}
}
);
Document document = reader.read(url);
// The document will now be complete but all the ROW elements
// will have been pruned.
// We may want to do some final processing now
...
</pre>
</dd>
</dl><dl>
<dt><a name="visitor">Does dom4j support the Visitor Pattern?</a></dt>
<dd>
<p>Yes. <i>dom4j</i> supports the visitor pattern via the
<a href="apidocs/org/dom4j/Visitor.html">Visitor</a> interface.
</p>
<p>Here is an example.
</p>
<pre>
protected void foo(Document doc) {
// lets use the Visitor Pattern to
// navigate the document for entities
Visitor visitor = new VisitorSupport() {
public void visit(Entity entity) {
System.out.println(
"Entity name: " + entity.getName()
+ " text: " + entity.getText()
);
}
};
doc.accept( visitor );
}
</pre>
</dd>
</dl><dl>
<dt><a name="sort-select">Can I sort the List returned by Node.selectNodes()?</a></dt>
<dd>
<p>Yes. The selectNodes() is a really useful feature to allow nodes to be
selected from any object in the <i>dom4j</i> object model via an XPath expression.
The List that is returned can be sorted by
specifying another XPath expression to use as the sorting comparator.
</p>
<p>
For example the following code parses an XML play and finds all the
SPEAKER elements sorted in name order.
</p>
<pre>
SAXReader reader = new SAXReader();
Document document = reader.read( new File( "xml/much_ado.xml" ) );
List speakers = document.selectNodes( "//SPEAKER", "." );
</pre>
<p>
In the above example the name of the SPEAKER is
defined by the XPath expression "." as the name is stored in the text
of the SPEAKER element.
If the name was defined by an attribute called "name" then
the XPath expression "@name" should be used for sorting.
</p>
<p>
You may wish to remove duplicates while sorting such that (for example)
the distinct list of SPEAKER elements is returned, sorted by name.
To do this add an extra parameter to the selectNodes() method call.
</p>
<pre>
List distinctSpeakers = document.selectNodes( "//SPEAKER", ".", true );
</pre>
</dd>
</dl><dl>
<dt><a name="whats-optional">What features are optional in dom4j?</a></dt>
<dd>
<p>In <i>dom4j</i> being able to navigate up a tree towards the parent
and to be able to change a tree are optional features.
These features are optional so that an implementation
can create memory efficient read only document models which
conserve memory by sharing imutable objects (such as interning
Atttributes).
</p>
<p>There are some helper methods to determine if optional features are
implemented.
Here is some example code demonstrating their use.
</p>
<pre>
protected void foo(Node node) {
// can we do upward navigation?
if ( ! node.supportsParent() ) {
throw new UnsupportedOperationException(
"Cannot navigate upwards to parent"
);
}
Element parent = node.getParent();
System.out.println( "Node: " + node
+ " has parent: " + parent
);
if ( parent != null ) {
// can I modify the parent?
if ( parent.isReadOnly() ) {
throw new UnsupportedOperationException(
"Cannot modify parent as it is read only"
);
}
parent.setAttributeValue( "bar", "modified" );
}
}
</pre>
</dd>
</dl><dl>
<dt><a name="why-jaxp-warning">What does the following mean 'Warning: Error occurred using JAXP to load a SAXParser. Will use Aelfred instead'</a></dt>
<dd>
<p>
If dom4j detects JAXP on the classpath it tries to use it to load a SAX parser.
If it can't load the SAX parser via JAXP it then tries to use the
<code>org.xml.sax.driver</code> system property to denote the SAX parser to use.
If none of the above work dom4j outputs a warning and continues, using its own
internal Aelfred2 parser instead.
</p>
<p>
The following warning is a result of JAXP being in the classpath but either
an old JAXP1.0 version
was found (rather than JAXP 1.1) or there is no JAXP configured parser
(such as crimson.jar or xerces.jar) on the classpath.
</p>
<pre>
Warning: Error occurred using JAXP to load a SAXParser. Will use Aelfred instead
</pre>
<p>
So the warning generally indicates an incomplete JAXP classpath and is
nothing to worry excessively about.
If you'd like to see the full verbose reason why the load of a JAXP
parser failed then you can try setting the system property
<code>org.dom4j.verbose=true</code>. e.g.
</p>
<pre>
java -Dorg.dom4j.verbose=true MyApp
</pre>
<p>And you should see a verbose list of why the load of a SAX parser
via JAXP failed.
</p>
<p>
To avoid this warning happening either remove the jaxp.jar
from your classpath or add a JAXP 1.1. jaxp.jar together with a JAXP 1.1
parser such as crimson.jar or xerces.jar
to your classpath.
</p>
</dd>
</dl><dl>
<dt><a name="what-parser">What XML parser does dom4j use?</a></dt>
<dd>
<p>dom4j works with any SAX parser via JAXP. So putting
a recent distribution of crimson.jar or xerces.jar on
the CLASSPATH will allow Crimson or Xerces's parser to be used.
</p>
<p>If no SAX parser is on the classpath via JAXP or the SAX
org.xml.sax.driver system property then the embedded Aelfred
distribution will be used instead. Note that the embedded
Aelfred distribution is a non validating parser, though it is quite fast
</p>
</dd>
</dl><dl>
<dt><a name="how-validate">How can I validate my document?</a></dt>
<dd>
<p>If a recent version of crimson.jar or xerces.jar is on the CLASSPATH
then dom4j will use that as the SAX parser via JAXP.
If none of these are on the CLASSPATH then a bundled version of Aelfred is used,
which does not validate.
</p>
<p>So to perform DTD validation when parsing put crimson.jar or xerces.jar on
the CLASSPATH. If you wish to validate against an XML Schema then try xerces.jar.
Then use the following code.</p>
<pre>
// turn validation on
SAXReader reader = new SAXReader(true);
Document document = reader.read( "foo.xml" );
</pre>
<p>
Note: if you want to validate against an XML Schema with xerces,
you need to enable the XML Schema validation with the "setFeature" method.
For more information about
<a href="http://xml.apache.org/xerces2-j/features.html" class="externalLink" title="External Link">xerces features</a>
visit the xerces website.
Below is a code sample to enable XML Schema validation.
</p>
<pre>
// turn validation on
SAXReader reader = new SAXReader(true);
// request XML Schema validation
reader.setFeature("http://apache.org/xml/features/validation/schema", true);
Document document = reader.read( "foo.xml" );
</pre>
<p>An alternative approach is to use Sun's MSV library for validation,
which allows you to use DTD, XML Schema, Relax NG, Relax or TREX as the
schema languages.
There's an example in the daily build at dom4j/src/samples/validate/JARVDemo.java
</p>
<p>If you are validating an existing dom4j document then we recommend
you try MSV as it avoids turning the document into text and then parsing
it again - MSV can work purely off of SAX events generated from the dom4j document.
</p>
<p>Using this approach your code will actually be based on the
<a href="http://iso-relax.sourceforge.net/JARV/" class="externalLink" title="External Link">JARV</a> API
which allows alternative validation mechanisms to
be plugged into your code.
</p>
</dd>
</dl><dl>
<dt><a name="import-vaj">How do I import dom4j into VAJ?</a></dt>
<dd>
<p>VisualAge for Java checks all dependencies in a JAR and displays
warnings if there are any unresolved links.
To avoid any warnings the following steps should be followed
(thanks to Jan Haluza for this).
</p>
<ol>
<li>Uninstall all the packages having anything in common with the xml
(com.ibm.xml* , org.w3c.dom ..) (these packages contains older
definitions DOM ver. 1 ...
</li>
<li>Install the following jars<pre>
dom4j.jar
xalan.jar
PullParser.jar
relaxng.jar
msv.jar
isorelax.jar
xsdlib.jar
crimson.jar
</pre>
</li>
</ol>
</dd>
</dl><dl>
<dt><a name="cannot-find-dtd">Cannot find DTD; how can I tell dom4j where to find the DTD from a DOCTYPE?</a></dt>
<dd>
<p>
A common way around this is to implement a SAX EntityResolver to load the
DTD from somewhere else. e.g. you could include the DTD in your JAR with
your java code and load it from there.
</p>
<pre>
EntityResolver resolver = new EntityResolver() {
public InputSource resolveEntity(String publicId, String systemId) {
if ( publicId.equals( "-//Acme//DTD Foo 1.2//EN" ) ) {
InputStream in = getClass().getResourceAsStream(
"com/acme/foo.dtd"
);
return new InputSource( in );
}
return null;
}
};
SAXReader reader = new SAXReader();
reader.setEntityResolver( resolver );
Document doc = reader.parse( "foo.xml" );
</pre>
</dd>
</dl></div></div></div><div class="clear"><hr></hr></div><div id="footer"><div class="xright">© 2001-2005, MetaStuff Ltd.</div><div class="clear"><hr></hr></div></div></body></html>
|