使用lxml向现有元素添加属性,删除元素等 [英] Adding attributes to existing elements, removing elements, etc with lxml
问题描述
我使用XML解析
from lxml import etree
tree = etree.parse('test.xml', etree.XMLParser())
现在,我要处理已解析的XML.我无法删除具有名称空间的元素,或者仅删除一般的元素,例如
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>
,我想删除整个元素以及标记中的所有内容.我也想向现有元素添加属性.我需要的方法在Element
类中,但是在这里我不知道如何将其与ElementTree
对象一起使用.任何指针都将不胜感激,谢谢
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
推荐答案
您可以通过以下调用进入根元素:root=tree.getroot()
You can get to the root element via this call: root=tree.getroot()
使用该根元素,您可以使用findall()
并删除符合条件的元素:
Using that root element, you can use findall()
and remove elements that match your criteria:
deleteThese = root.findall("title")
for element in deleteThese: root.remove(element)
最后,您可以使用以下代码查看新树的外观:etree.tostring(root, pretty_print=True)
Finally, you can see what your new tree looks like with this: etree.tostring(root, pretty_print=True)
以下是有关find/findall工作方式的一些信息: http://infohost.nmt.edu/tcc /help/pubs/pylxml/class-ElementTree.html#ElementTree-find
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:
root.attrib['myNewAttribute']='hello world'
这篇关于使用lxml向现有元素添加属性,删除元素等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!