Skip to content
Advertisement

Loading XML into DOmSource is adding xmlns=”” to all bean tags

I am trying to programmatically edit an XML file by loading it into a Document object and manipulating it there. This portion on the program works however if I load the XML into either an InputStream or write it back to a file, all the bean tags have xmlns="" attribute added to them.

So if before I had:

JavaScript

I get back out:

JavaScript

This happens to all bean tags weather or not they are edited. I verified that simply reading the XML file and writing it back out with the following code without making any changes to the Document object still causes the error.

I assume this is happening when I create the DOMSource object but do not know how to stop this from happening.

Currently these added attributes are causing errors when I try to use the XML file.

JavaScript

Advertisement

Answer

Firstly, add

JavaScript

Secondly, make sure that whenever you add an element programmatically to the DOM, you use the namespace-aware versions of the methods, for example createElementNS().

Consider using a library such as XOM or JDOM2 in preference to DOM. The popularity of DOM never ceases to astound me when far better alternatives are available.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement