如何使用 ElementTree for python 读取带有 </xliff:g> 标签的所有文本

How to read all text with </xliff:g> tag by using ElementTree for python

我正在尝试阅读和检查 Android string.xml 文件中的所有文本。

文件包含以下内容:

<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<string name="status">Finishing <xliff:g id="number">%d</xliff:g> percent.</string>
</resources>

我试过了:

import xml.etree.ElementTree as ET
tree = ET.parse(filepath)
root = tree.getroot()

print len(root[0])
# it prints 1

print root[0].text
# it prints Finishing 

print root[0][0].text
#it prints %d

如何找到打印第三个文本的方法,"percent.",而 <xliff:g> 标签挡住了我的路。 有什么正确的方法吗?谢谢

ElementTree 中,您的目标文本 "percent." 被建模为元素 xliff:gtail :

>>> ns = {"xliff": "urn:oasis:names:tc:xliff:document:1.2"}
>>> g = root.find("string/xliff:g", namespaces=ns)
>>> print g.tail
 percent.

或使用更接近您尝试的代码的东西:

>>> print root[0][0].tail
 percent.