在python中检索元素的所有文本,包括其子元素 [英] Retrieve all text of an element including its child in python
本文介绍了在python中检索元素的所有文本,包括其子元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我编写了一个代码来查找 xml 中特定标签中的文本.它适用于没有子标签的标签.
i wrote a code to find text in specific tags in an xml. It works fine for tags without a child tag.
For e.g. 1 <a>ajsaka</a>. it works fine for this.
e.g. 2 But if there is an instance of <b>ahsjd<c>jjiij</c>aa</b>.
它不起作用.我想要标签中的所有内容,包括其子元素文本.我希望它打印 ahsjdjjiijaa,但它只打印 ahsjd.到目前为止,这是我的代码.
It doesnt work. I want all the content in a tag including its child element text. I want it to print ahsjdjjiijaa, but instead it prints only ahsjd. Here is my code so far.
这是输入文件.
<level>
<ex>
<nt>[edit <topic-ref link-text="short-title"
topic-id="13629">address</topic-ref>],</nt>
<nt>[edit routing-instances <var>routing-instance-name</var
> <topic-ref link-text="short-title" topic-id="13629">address-
assignment</topic-ref
>]</nt>
</ex>
<exam>
</exam>
</level>
from lxml import etree
doc=etree.parse('C:/xx/bb.xml')
root=doc.getroot()
node=root.find('level')
count=len(node.getchildren())
print (count)
for elem in root.findall('level/ex/nt'):
print (elem.text)
我如何获得它?
推荐答案
您可以将文件作为字符串读取,然后在标签之间连接所有文本
You can read your file as string, then concatinate all text between tags
import xml.etree.ElementTree as ET
text = open('C:/xx/bb.xml').read()
''.join(ET.fromstring(text).itertext())
输出:
'ahsjdjjiijaa'
这篇关于在python中检索元素的所有文本,包括其子元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文