每当我打电话时ElementTree.tostring(e),都会收到以下错误消息:
AttributeError: 'Element' object has no attribute 'getroot'
Run Code Online (Sandbox Code Playgroud)
有没有其他方法将ElementTree对象转换为XML字符串?
追溯:
Traceback (most recent call last):
File "Development/Python/REObjectSort/REObjectResolver.py", line 145, in <module>
cm = integrateDataWithCsv(cm, csvm)
File "Development/Python/REObjectSort/REObjectResolver.py", line 137, in integrateDataWithCsv
xmlstr = ElementTree.tostring(et.getroot(),encoding='utf8',method='xml')
AttributeError: 'Element' object has no attribute 'getroot'
Run Code Online (Sandbox Code Playgroud) 我需要在更改后将XML ElementTree转换为String.这是toString部分无法正常工作.
import xml.etree.ElementTree as ET
tree = ET.parse('my_file.xml')
root = tree.getroot()
for e in root.iter('tag_name'):
e.text = "something else" # This works
# Now I want the the complete XML as a String with the alteration
Run Code Online (Sandbox Code Playgroud)
我已经尝试了以下各种版本的版本,ET或ElementTree作为各种名称,并导入toString等等,
s = tree.tostring(ET, encoding='utf8', method='xml')
Run Code Online (Sandbox Code Playgroud)
我已经看到将Python Python ElementTree转换为字符串和其他一些,但我不确定如何将它应用于我的示例.