我編寫了一個代碼來查找xml中特定標記中的文本。它適用於沒有子標籤的標籤。檢索一個元素的所有文本,包括其python中的子元素
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。這是我的代碼到目前爲止。
這裏是輸入文件。
<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)
我該如何得到它?
這裏沒有'你輸入XML中level'標籤。擴展您的輸入 – RomanPerekhrest