是否有一种方法从文件中解析整个ElementTree并将其作为python中的字符串返回?我想将整个文件读取为单个字符串值,例如抓取转储(树)的整个输出?任何帮助或建议将非常感激!
xmlimport xml.etree.ElementTree as ET
print "Enter a filename"
filename = input()
tree = ET.parse(filename)
string = tree.tostring() ##is there a way to do something like this?
test.xml
<data>
<serial>
<serial name = "serial">SN001</serial>
</serial>
<items>
<item>Test1 = Failed</item>
<item>Test2 = Passed</item>
<item>Test3 = Passed</item>
</items>
</data>
tostring
是一个模块函数,不是一个方法。
string = ET.tostring(tree.getroot())