ElementTree和Element有什么区别?(python xml) [英] What is the difference between a ElementTree and an Element? (python xml)

查看:73
本文介绍了ElementTree和Element有什么区别?(python xml)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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)会产生相同的结果.因此,我很难确定两者之间的区别.

In the code above, dumping tree (which is an ElementTree) and dumping elem (which is an Element) results in the same thing. Therefore I am having trouble determining what the difference is between the two.

推荐答案

倾销树(是ElementTree)和倾销elem(是Element)会导致相同的结果.

dumping tree (which is an ElementTree) and dumping elem (which is an Element) results in the same thing.

dump() 函数对 ElementTree Element 的作用相同,因为它是故意使这种行为:

dump() function works the same for ElementTree and Element because it was intentionally made to behave this way:

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 接口的更大"的类.

ElementTree is a wrapper class that corresponds to the "entire element hierarchy" providing serialization functionality - dumping and loading the tree. Element, on the other hand, is a much "bigger" class that defines the Element interface.

这篇关于ElementTree和Element有什么区别?(python xml)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆