转储所有XPath

时间:2016-09-28 19:17:52

标签: python xml xpath

lxml或exml是否具有导出XML中所有xpath的功能?

XML示例:

 <note>
    <to>Tove</to>
    <from>Jani</from>
    <heading>Reminder</heading>
    <body>
         <content>Don't forget me this weekend!</content>
    </body>
    </note>

XPath结果:

\\note
\\note\to
\\note\from
\\note\heading
\\note\body
\\note\body\content

1 个答案:

答案 0 :(得分:1)

您必须遍历树并在每个节点上调用getpath

null

输出:

x = """<note>
    <to>Tove</to>
    <from>Jani</from>
    <heading>Reminder</heading>
    <body>
         <content>Don't forget me this weekend!</content>
    </body>
    </note>"""

from lxml import etree
from StringIO import StringIO
tree = etree.parse(StringIO(x))

paths = "\n".join(tree.getpath(n) for n in tree.iter())
print(paths)