Currently using Python 2.4.3, and not allowed to upgrade
I want to change the values of a given attribute in one or more tags, together with XML-comments in the updated file.
I have managed to create a Python script that takes a XML-file as argument, and for each tag specified changes an attribute, as shown below
def update(file, state):
global Etree
try:
from elementtree import ElementTree
print '*** using ElementTree'
except ImportError, e:
print '***'
print '*** Error: Must install either ElementTree or lxml.'
print '***'
raise ImportError, 'must install either ElementTree or lxml'
#end try
doc = Etree.parse(file)
root = doc.getroot()
for element in root.findall('.//StateManageable'):
element.attrib['initialState'] = state
#end for
doc.write(file)
#end def
This is all fine, the attributes "initialState" are updated, except for the fact that my original XML contains a lot of XML comments as well, but they are long gone, which is bad.
Suspect that parse only retrieves the XML-structure, but I thought XML-comments where a part of the structure. I also realize that the "human-readable" formatting of my original document is long gone, but that I have realized is expected behavior, need to format afterwards using xmllint --format
or XSL.