私は Python を学んでいますが、xml パーサー (ElementTree - XMLParser) の動作を理解するのが難しいです。
ドキュメントの例を変更しました
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>
私の質問は
- data() メソッドが呼び出されるタイミング。
- 開始タグの前に 2 回呼び出されるのはなぜですか
data
メソッドの詳細を取得するための API ドキュメントが見つかりませんでした。XMLParser
クラスの API リファレンスのような javadoc はどこにありますか。