如何插入带有文本的元素?

问题描述 投票:0回答:2

我可以插入这样的元素:

product_node.insert(cnt, etree.Element("vod_type"))

是否可以执行以下操作:

product_node.insert(cnt, etree.Element("vod_type").text="hello")

或者类似的东西。或者我必须把它分成三行吗?

elem = etree.Element("vod_type")
elem.text = "hello"
product_node.insert(cnt, elem)
python python-3.x xml lxml
2个回答
0
投票

ElementMaker 可用于一步完成此操作

from lxml.builder import E

product_node.insert(cnt, E("vod_type", "hello"))

0
投票

从您正在使用的 Element

 类的 
文档中
text
可以作为关键字参数提供。

product_node.insert(cnt, etree.Element("vod_type", text="hello"))
© www.soinside.com 2019 - 2024. All rights reserved.