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

Jason Hunter jhunter at acm.org
Wed Dec 5 16:26:51 PST 2001


This was a bug in the Element class, something that isn't triggered
during SAX builds but only during DOM builds, probably due to the order
of additions to the document being different.  I just checked in a fix. 
You can get the code from CVS.

Some people suggested this might be malformed XML, but it's quite all
right.  Just a gotcha in DOM building.  Wouter, you may want to switch
to SAX building since it's faster and of course more thoroughly tested
since it's the preferred build mechanism.

-jh-

Wouter Cordewiner wrote:
> 
> 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
> _______________________________________________
> To control your jdom-interest membership:
> http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhost.com



More information about the jdom-interest mailing list