Code
When running the following code I cannot save the output as an xml file as I get the following error AttributeError: 'ElementTree' object has no attribute 'tag'(in traceback). There is a similarly named question on SO but I do not believe it is relevant to mine as it was to do with parsing from the root node, not saving.
Code
import xml.etree.ElementTree as ET
print('\n'*5)
xmlfile = 'widget.XML'
tree = ET.parse(xmlfile)
root = tree.getroot()
#ET.dump(tree)# prints the xml file to console,shows xml indentation
print('\n'*2)
for elm in root.findall("./Common/ForceBinary"):
print(elm.attrib)
elm.attrib = {'type': 'integer', 'value': '0'}
with open("new_file.xml", "w") as f:
f.write(ET.tostring(tree))
Traceback
{'type': 'integer', 'value': '1'}
Traceback (most recent call last):
File "/Users/user/Desktop/MY_PY/x_08.py", line 16, in <module>
f.write(ET.tostring(tree))
File "/Users/user/opt/anaconda3/lib/python3.7/xml/etree/ElementTree.py", line 1136, in tostring
short_empty_elements=short_empty_elements)
File "/Users/user/opt/anaconda3/lib/python3.7/xml/etree/ElementTree.py", line 777, in write
short_empty_elements=short_empty_elements)
File "/Users/user/opt/anaconda3/lib/python3.7/xml/etree/ElementTree.py", line 901, in _serialize_xml
tag = elem.tag
AttributeError: 'ElementTree' object has no attribute 'tag'