首先,考虑使用以下lxml
实现ElementTree
:
http://lxml.de/
这是 libxml2 的包装器,我发现它执行得很好。
以交互方式运行 python,对同一个 etree 对象进行多个查询。ipython
是一个增强的交互式 python 解释器,可以轻松访问自省和便利语法。
例如,使用ipython 以交互方式检查note.xmllxml.etree
。
$ ipython
Python 2.5.1 (r251:54863, Jul 10 2008, 17:24:48)
Type "copyright", "credits" or "license" for more information.
IPython 0.8.2 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object'. ?object also works, ?? prints more.
In [1]: from lxml import etree
In [2]: doc = etree.parse(open("note.xml"))
In [3]: etree.dump(doc.getroot())
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
In [4]: doc.xpath('/note/*')
Out[4]:
[<Element to at 89cf02c>,
<Element from at 89cf054>,
<Element heading at 89cf07c>,
<Element body at 89cf0a4>]