如何使用文本内的标记解析xml语句

时间:2015-09-03 18:21:53

标签: python xml xml-parsing elementtree

如何在python中解析以下内容以获取<p>括号但没有<mark>标记或值的文本?最好使用ElementTree功能

<plist>
<p>Hello there? <mark type="ph"/> How are you?</p>
</plist>

解析以下内容应该返回'你好吗?你好吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

如果是lxml.etree,您可以使用string() XPath函数:

from lxml.etree import fromstring

data = """
<plist>
<p>Hello there? <mark type="ph"/> How are you?</p>
</plist>
"""

root = fromstring(data)
for p in root.xpath("//plist/p"):
    print p.xpath("string()")

打印:

Hello there?  How are you?