[jdom-interest] DOMBuilder, DOM2JDom again.

Xuemin Guan xuemin at appresso.com
Fri Sep 21 05:10:34 PDT 2001


Hi, all,

Actually, I posted this problem last week. Sorry to crop up it again. But
I haven't got some certain answers back.

I think there is a bug in the DOMBuilder when it converts a DOM tree
to a JDOM tree. I need to convert between a JDom treen and a DOM
tree forth and back a few times. Error happens after the first conversion,
here is the error message:

Exception in thread "main" org.jdom.IllegalAddException: The namespace xmlns:="u
rn:biztalk-org:biztalk:biztalk_1" could not be added as content to "biztalk_1":
The namespace prefix "" collides with the element namespace prefix
         at org.jdom.Element.addNamespaceDeclaration(Element.java:367)
         at org.jdom.input.DOMBuilder.buildTree(DOMBuilder.java:417)
         at org.jdom.input.DOMBuilder.buildTree(DOMBuilder.java:377)
         at org.jdom.input.DOMBuilder.build(DOMBuilder.java:338)
         at test.main(test.java:20)

Currently, I serialize the XML file to harddisk to work around this problem.

The below is a testing source code, you can try it out yourself:

import org.w3c.dom.*;

import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;

import java.io.File;

class test{
	public static void main(String args[] )
		throws Exception
	{
	  if(args.length != 1){
	   System.out.println("Usage: java test{"+
		"[XML Document filename]");
	   return;
	  }
	 SAXBuilder saxBuilder = new SAXBuilder();
	 org.jdom.Document jdomDoc = saxBuilder.build(new File(args[0]));

         DOMOutputter outputer = new DOMOutputter();

		org.w3c.dom.Document domDoc =outputer.output(jdomDoc);
         DOMBuilder domBuilder = new DOMBuilder();
         jdomDoc = domBuilder.build(domDoc);

        domDoc = outputer.output(jdomDoc);

        jdomDoc = domBuilder.build(domDoc);
      }
  }

If you try it, you would fine the first conversion is just fine, but when 
the program
tries to convert between a JDom tree and DOM tree again, the test.class throw
the above exception. I had a look at the JDom source, I believe it is caused by
a bug.

The follows is the XML document I used with the above java code.

<?xml version="1.0" encoding="Shift_JIS"?>
<biztalk_1 xmlns="urn:biztalk-org:biztalk:biztalk_1">
     <header>
         <delivery>
             <to>
                 <address>urn:sap-com:logical-system:SAP</address>
             </to>
             <from>
                 <address>urn:sap-com:logical-system:EXTERNAL</address>
             </from>
         </delivery>
     </header>
     <body xmlns="">
         <doc:Customer.Search.Response xmlns:doc="urn:sap-com:document:sap:business">
             <Multiple>
                 <item>
                     <CUSTOMER>10001</CUSTOMER>
                 </item>
                 <item>
                     <CUSTOMER>10002</CUSTOMER>
                 </item>
                 <item>
                     <CUSTOMER>10003</CUSTOMER>
                 </item>
                 <item>
                     <CUSTOMER>10004</CUSTOMER>
                 </item>
                 <item>
                     <CUSTOMER>10005</CUSTOMER>
                 </item>
                 <item>
                     <CUSTOMER>10006</CUSTOMER>
                 </item>
             </Multiple>
             <PeCustomer>aaa</PeCustomer>
             <Return>
                 <MESSAGE>Looks good.</MESSAGE>
             </Return>
         </doc:Customer.Search.Response>
     </body>
</biztalk_1>

Thanks for your time.
-------
Xuemin Guan
Software Engineer
Appresso SpA (www.appresso.com)
Tel:  +81-3-4412-7790(Direct)
        +81-3-4412-7700(Represent)
Email: xuemin at appresso.com 




More information about the jdom-interest mailing list