[jdom-interest] JDOM, Xerces and Crimson with namepsaces

Wouter Cordewiner wcordewiner at hotmail.com
Tue Nov 27 06:47:40 PST 2001


Hi,

I encountered an issue that seems to depend on the XML parser I use.

Below a reproducable (XML file and Java code) to illustrate the issue I
encounter:

File "ns.xml":

<?xml version="1.0" encoding="UTF-8" ?>

<root xmlns:ns1="http://namespace/ns1">

  <ns1:el1 xmlns="http://namespace/ns" att1="Attribute 1" />

</root>

Java code:

public class NSTest {
 public static void main(String[] args) {
  try {
   // Create JDOM tree.
   org.jdom.input.DOMBuilder db = new org.jdom.input.DOMBuilder();
   org.jdom.Document doc = db.build ( new java.io.File ( "ns.xml" ) );
  } catch ( Throwable ex ) {
   ex.printStackTrace();
  }
 }
 private NSTest() {}
}

When I run the NSTest class using the Crimson parser, the file gets loaded.

If I use the Xerces parser, I get following exception:

org.jdom.JDOMException: Error in building from stream: The namespace
xmlns:="http://namespace/ns" could not be added as content to "ns1:el1": The
namespace prefix "" collides with an attribute namespace on the element
 at org.jdom.input.DOMBuilder.build(DOMBuilder.java:279)
 at org.jdom.input.DOMBuilder.build(DOMBuilder.java:300)
 at NSTest.main(NSTest.java:7)
Root cause: org.jdom.IllegalAddException: The namespace
xmlns:="http://namespace/ns" could not be added as content to "ns1:el1": The
namespace prefix "" collides with an attribute namespace on the element
 at org.jdom.Element.addNamespaceDeclaration(Element.java:391)
 at org.jdom.input.DOMBuilder.buildTree(DOMBuilder.java:417)
 at org.jdom.input.DOMBuilder.buildTree(DOMBuilder.java:459)
 at org.jdom.input.DOMBuilder.buildTree(DOMBuilder.java:377)
 at org.jdom.input.DOMBuilder.build(DOMBuilder.java:262)
 at org.jdom.input.DOMBuilder.build(DOMBuilder.java:300)
 at NSTest.main(NSTest.java:7)

I looked into it, and I think the problem lies in the difference between the
Crimson and Xerces parser on the implementation of the NamedNodeMap
interface of both parsers.
The NamedNodeMap object returned from the getAttributes() method on the
ns1:el element is causing the problem.
If you call the item(int) method on the NamedNodeMap object, Xerces and
Crimson return the attributes in a different order.

Any ideas/suggestions on how to solve this?

Thank you,

Wouter Cordewiner



More information about the jdom-interest mailing list