9
如何在Python中将ElementTree
作为字符串获取?获取Python xml的字符串源ElementTree
如何在Python中将ElementTree
作为字符串获取?获取Python xml的字符串源ElementTree
import xml.etree.ElementTree as ET
tree = ET.parse(source)
root = tree.getroot()
ET.tostring(root)
注意,有可能是格式化的source
和ET.tostring(doc)
内容之间的差异。
'ElementTree'对象没有属性'tostring'发生在我试过这个时 – James
@James:那么你就是在错误的对象上调用它。这是*模块*具有该方法。 –
@James:如果'tree'是一个'ElementTree'(我的错误),那么'ET.tostring(tree)'不起作用。相反,用'root = tree.getroot()'得到树的根,然后调用'ET.tostring(root)'。 – unutbu