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(它是一个Element)导致相同的事情.因此,我无法确定两者之间的差异.
解决方法:
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接口的“更大”的类.
标签:python,xml,element,elementtree
来源: https://codeday.me/bug/20190628/1316615.html