from xml.etree.ElementTree import ElementTree, Element, SubElement, dump
elem = Element('1')
sub = SubElement(elem, '2')
tree = ElementTree(elem)
dump(tree)
dump(elem)
在上面的代码中,转储树(它是一个 ElementTree)和转储 elem(它是一个元素)产生相同的结果。因此,我无法确定两者之间的区别。
最佳答案
dumping tree (which is an ElementTree) and dumping elem (which is an Element) results in the same thing.
dump()
函数对 ElementTree
和 Element
的作用相同,因为它是有意设计成这样的:
def dump(elem):
# debugging
if not isinstance(elem, ElementTree):
elem = ElementTree(elem)
elem.write(sys.stdout)
...
I am having trouble determining what the difference is between the two.
ElementTree
是一个包装类,对应于提供序列化功能的“整个元素层次结构”——转储和加载树。 Element
,另一方面,是一个“更大”的类,它定义了 Element
接口(interface)。
关于python - ElementTree 和 Element 有什么区别? ( python XML),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30813156/