I need to change some elements of an XML file and write the file with no other differences.
In detail I have a set of elements which need to have an id attribute in the xml code. I find these elements with a xpath expression and add an ID to it. But when the dom is written again, the formatting differs a bit.
- the order of the attributes is changes to alphabetical
- the definition of the namespace is moved to the elements (
<ns1:root xmlns:ns1="abc" xmlns:ns2="xzy"><ns2:element/></ns1:root>
changes to<root xmlns="abc"><element xmlns="xzy"/></root>
) - linebreaks and indetion change
The xml is read with javax.xml.parsers.SAXParser (namespaceaware: true) and written with javax.xml.transform.TransformerFactory (indent: yes).
The best way to preserve the formatting would be to alter the source string, is there a good way to do this without diving in too deep into the xml parsing thing?
Or is there a way to parse the xml to dom whitespace aware?
No comments:
Post a Comment