Package: org.apache.soap.util.xml.DOM2Writer Problem: In some cases, the DOM2Writer will output duplicate XML namespace declarations on a single element, thereby making the XML output invalid.
Example, let's say I want to create the following XML output: <s:test xmlns:s="testing> <hello /> </s:test> To do so, I use the following code. Document doc = ... create document Element test = doc.createElementNS("testing", "test"); test.setPrefix("s"); test.setAttribute("xmlns:s", "testing"); Element hello = doc.createElementNS("testing", "hello"); test.appendChild(hello); doc.appendChild(test); Run this through the DOM2Writer, and the output is: <s:test xmlns:s="testing" xmlns:s="testing"> <hello /> </s:test> Notice the duplicate xmlns:s declarations. This is caused by the fact that the xmlns:s declaration is set as an attribute on the element. DOM2Writer checks to see if the element namespace and prefix have been declared before serializing all of the attributes. In this case, if the "s" prefix for the "testing" namespace has not been declared, the xmlns:s="testing" is added to the XML output. Then, however, the attributes are looped through and serialized, causing a duplicate declaration to be printed. Why am I adding the test.setAttribute("xmlns:s", "testing")? Because that's about the only ways to declare namespaces in the DOM API. I could get around this problem by simply removing the test.setAttribute("xmlns:s", "testing") line, but unfortunately, I don't have access to the code that creates the real DOM document I'm trying to work with (the above is just an example). So the setAttribute line remains and we have to work around the problem within the DOM2Writer. Proposed Solution: When looping through the attributes, check to see if the attribute is an XML namespace declaration, and if so, whether or not the namespace has already been declared. If so, skip it and move on. The proposed modified DOM2Writer is attached. Can somebody please review to make sure that my head is screwed on straight and I didn't overlook something before I commit it. - James Snell Software Engineer, Internet Emerging Technologies, IBM James M Snell/Fresno/IBM - [EMAIL PROTECTED] These things I have spoken to you, so that in Me you may have peace. In the world you have tribulation, but take courage; I have overcome the world. - John 16:33
DOM2Writer.java
Description: Binary data