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

Eric A. Sirois easirois at hotmail.com
Tue Nov 27 14:41:39 PST 2001


 Hello,

The example below should work for you.  For element "el1" you assigned it the default namespace "http://namespace/ns" but added the prefix for the ns1 prefix.  The error message you received is correct.  It's saying I don't understand the prefix "ns1" for element "el1" you just assigned it with a default namespace.When you declare/assign a namespace it is associated with the current element and any child element not including attributes.
<?xml version="1.0" encoding="UTF-8" ?><ns1:root xmlns:ns1="http://namespace/ns1">  <el1 xmlns="http://namespace/ns" att1="Attribute 1" /></ns1:root>
namespace     element
        ns1             root
       default          el1

If  you need the  XML as-is try

<?xml version="1.0" encoding="UTF-8" ?><root xmlns="http://namespace/ns">  <ns1:el1 xmlns:ns1="http://namespace/ns1" att1="Attribute 1" /></root>


HTH,

Eric

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 Iencounter: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 namespacexmlns:="http://namespace/ns" could not be added as content to "ns1:el1": Thenamespace 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 namespacexmlns:="http://namespace/ns" could not be added as content to "ns1:el1": Thenamespace 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 theCrimson and Xerces parser on the implementation of the NamedNodeMapinterface of both parsers.The NamedNodeMap object returned from the getAttributes() method on thens1:el element is causing the problem.If you call the item(int) method on the NamedNodeMap object, Xerces andCrimson 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


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://jdom.org/pipermail/jdom-interest/attachments/20011127/4687c2e3/attachment.htm


More information about the jdom-interest mailing list