我正在尝试让我的脚本正常工作。到目前为止,它没有设法输出任何东西。
这是我的 test.xml
<mediawiki xmlns="http://www.mediawiki.org/xml/export-0.8/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mediawiki.org/xml/export-0.8/ http://www.mediawiki.org/xml/export-0.8.xsd" version="0.8" xml:lang="it">
<page>
<title>MediaWiki:Category</title>
<ns>0</ns>
<id>2</id>
<revision>
<id>11248</id>
<timestamp>2003-12-31T13:47:54Z</timestamp>
<contributor>
<username>Frieda</username>
<id>0</id>
</contributor>
<minor />
<text xml:space="preserve">categoria</text>
<sha1>0acykl71lto9v65yve23lmjgia1h6sz</sha1>
<model>wikitext</model>
<format>text/x-wiki</format>
</revision>
</page>
</mediawiki>
这是我的代码
from lxml import etree
def fast_iter(context, func):
# fast_iter is useful if you need to free memory while iterating through a
# very large XML file.
#
# http://www.ibm.com/developerworks/xml/library/x-hiperfparse/
# Author: Liza Daly
for event, elem in context:
func(elem)
elem.clear()
while elem.getprevious() is not None:
del elem.getparent()[0]
del context
def process_element(elem):
if elem.ns.text == '0':
print elem.title.text
context=etree.iterparse('test.xml', events=('end',), tag='page')
fast_iter(context, process_element)
我没有收到任何错误,只是没有输出。我想要得到的是解析元素是否为0。