[英]Finding sub elements of sub elements in xml python
I have an xml file that is containing a root element, and sub elements of sub elements. 我有一个xml文件,其中包含根元素和子元素的子元素。
For example: 例如:
<root>
<subRoot>
<Modified name="Set" text="Bla">
<Action name="Bla2"/>
</Modified>
</subRoot>
</root>
If i want to delete all of the Action tags under Modified- How can i do that? 如果我想删除“已修改”下的所有“动作”标签,该怎么办? Thanks.
谢谢。
You can use various libraries, this example is using beautifulsoup
: 您可以使用各种库,此示例使用
beautifulsoup
:
data = '''<root>
<subRoot>
<Modified name="Set" text="Bla">
<Action name="Bla2"/>
</Modified>
</subRoot>
</root>'''
from bs4 import BeautifulSoup
soup = BeautifulSoup(data, 'xml')
# selects all tags <Action> under <Modified>
for tag in soup.select('Modified Action'):
tag.extract() # delete it!
print(soup.prettify())
Prints: 印刷品:
<?xml version="1.0" encoding="utf-8"?>
<root>
<subRoot>
<Modified name="Set" text="Bla">
</Modified>
</subRoot>
</root>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.