使用 lxml 向现有元素添加属性、删除元素等
我使用“
from lxml import etree
tree = etree.parse('test.xml', etree.XMLParser())
现在我想处理已解析的 XML”来解析 XML。我在删除具有命名空间的元素或仅删除一般元素时遇到问题,例如
<rdf:description><dc:title>Example</dc:title></rdf:description>
我想删除整个元素以及标签内的所有内容。我还想向现有元素添加属性。我需要的方法位于 Element 类中,但我不知道如何将其与此处的 ElementTree 对象一起使用。任何指点都将不胜感激,谢谢
I parse in the XML using
from lxml import etree
tree = etree.parse('test.xml', etree.XMLParser())
Now I want to work on the parsed XML. I'm having trouble removing elements with namespaces or just elements in general such as
<rdf:description><dc:title>Example</dc:title></rdf:description>
and I want to remove that entire element as well as everything within the tags. I also want to add attributes to existing elements as well. The methods I need are in the Element
class but I have no idea how to use that with the ElementTree
object here. Any pointers would definitely be appreciated, thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过以下调用获取根元素:
root=tree.getroot()
使用该根元素,您可以使用
findall()
并删除符合您条件的元素:最后,您可以使用以下命令查看新树的外观:
etree.tostring(root, Pretty_print=True)
以下是有关 find/findall 如何工作的一些信息:
http://infohost.nmt.edu/tcc /help/pubs/pylxml/class-ElementTree.html#ElementTree-find
要向元素添加属性,请尝试如下操作:
You can get to the root element via this call:
root=tree.getroot()
Using that root element, you can use
findall()
and remove elements that match your criteria:Finally, you can see what your new tree looks like with this:
etree.tostring(root, pretty_print=True)
Here is some info about how find/findall work:
http://infohost.nmt.edu/tcc/help/pubs/pylxml/class-ElementTree.html#ElementTree-find
To add an attribute to an element, try something like this:
remove
方法应该执行您想要的操作:The
remove
method should do what you want: