Element类

Element类是包含ElementTree API的主要object,包括跟XML树相关的函数

from lxml import etree

root = etree.Element("root") #创建根节点
root.append(etree.Element("child1"))  #创建子节点child1

child2 = etree.SubElement(root, "child2") #创建子节点child2
child3 = etree.SubElement(root, "child3") #创建子节点child3

print(etree.tostring(root, pretty_print=True)) #输出所有节点

猜你喜欢

转载自www.cnblogs.com/shiliye/p/11753282.html