Changing XML while saving layout

What would you use to modify the XML file, preserving as much layout as possible, including indentation and comments?

My problem is that I have several massive manually edited XML files that describe the user interface, and now I need to translate several attributes into another language.

I tried doing this with Python + ElementTree, but it did not save any spaces or comments.

I saw that XSLT is suggested for similar questions, but I don’t think it is an alternative in this case, since I need to do some logic and search for each attribute.

It would be preferable if the order of the attributes in each element is also preserved, but I can tolerate the reordered order.

+4
source share
2 answers

Any DOM manipulation module should suit your needs. A layout is just text data, so it represents text nodes in the DOM:

>>> from xml.dom.minidom import parseString >>> dom = parseString('''\ ... <message> ... <text> ... Hello! ... </text> ... </message>''') >>> dom.childNodes[0].childNodes [<DOM Text node "u'\n '">, <DOM Element: text at 0xb765782c>, <DOM Text node "u'\n'">] >>> text = dom.getElementsByTagName('text')[0].childNodes[0] >>> text.data = text.data.replace(u'Hello', u'Hello world') >>> print dom.toxml() <?xml version="1.0" ?><message> <text> Hello world! </text> </message> 
+2
source

If you use an XSLT processor such as xt , you can write extension methods in Java that can perform any arbitrary conversion necessary.

Having said that, I have successfully used Python xml.dom.minidom for this kind of conversion. It saves spaces and layout.

+1
source

All Articles