0
我在学习Python,并且对XML解析器(ElementTree-XMLParser)行为有一些困难的理解。Python XMLParser:什么时候是data()方法调用
我修改的例子在documentation
class MaxDepth: # The target object of the parser
path = ""
def start(self, tag, attrib): # Called for each opening tag.
self.path += "/"+ tag
print '>>> Entering - ' + self.path
def end(self, tag): # Called for each closing tag.
print '<<< Leaving - ' + self.path
if self.path.endswith('/'+tag):
self.path = self.path[:-(len(tag)+1)]
def data(self, data):
if data:
print '... data called ...'
print data , 'length -' , len(data)
def close(self): # Called when all data has been parsed.
return self
它输出下面输出
>>> Entering - /a
... data called ...
length - 1
... data called ...
length - 2
>>> Entering - /a/b
... data called ...
length - 1
... data called ...
length - 2
<<< Leaving - /a/b
... data called ...
length - 1
... data called ...
length - 2
>>> Entering - /a/b
... data called ...
length - 1
... data called ...
length - 4
>>> Entering - /a/b/c
... data called ...
length - 1
... data called ...
length - 6
>>> Entering - /a/b/c/d
... data called ...
length - 1
... data called ...
length - 6
<<< Leaving - /a/b/c/d
... data called ...
length - 1
... data called ...
length - 4
<<< Leaving - /a/b/c
... data called ...
length - 1
... data called ...
length - 2
<<< Leaving - /a/b
... data called ...
length - 1
<<< Leaving - /a
<__main__.MaxDepth instance at 0x10e7dd5a8>
我的问题是
- 当是()方法调用的数据。
- 为什么在开始标记之前调用两次
- 我无法找到api文档以获取有关
data
方法的更多详细信息。我在哪里可以找到类似XMLParser
类的api参考javadoc。
如果您的使用不需要事件解析,则使用'.parse()'http://www.doughellmann.com/PyMOTW/xml/etree/ElementTree/parse.html更容易。否则,他的事件示例可能会有所帮助:http://www.doughellmann.com/PyMOTW/xml/etree/ElementTree/parse.html#watching-events-while-parsing – ninMonkey