"xml.dom.pulldom" --- 部分的な DOM ツリー構築のサポート
*******************************************************

**Source code:** Lib/xml/dom/pulldom.py

======================================================================

The "xml.dom.pulldom" module provides a "pull parser" which can also
be asked to produce DOM-accessible fragments of the document where
necessary. The basic concept involves pulling "events" from a stream
of incoming XML and processing them. In contrast to SAX which also
employs an event-driven processing model together with callbacks, the
user of a pull parser is responsible for explicitly pulling events
from the stream, looping over those events until either processing is
finished or an error condition occurs.

注釈:

  If you need to parse untrusted or unauthenticated data, see XML
  security.

バージョン 3.7.1 で変更: SAXパーサーは、デフォルトでセキュリティーを向
上させるために、一般的な外部エンティティーをデフォルトでは処理しなくな
りました。外部エンティティの処理を有効にするには、次の場所にカスタムパ
ーサーインスタンスを渡します:

   from xml.dom.pulldom import parse
   from xml.sax import make_parser
   from xml.sax.handler import feature_external_ges

   parser = make_parser()
   parser.setFeature(feature_external_ges, True)
   parse(filename, parser=parser)

以下はプログラム例です:

   from xml.dom import pulldom

   doc = pulldom.parse('sales_items.xml')
   for event, node in doc:
       if event == pulldom.START_ELEMENT and node.tagName == 'item':
           if int(node.getAttribute('price')) > 50:
               doc.expandNode(node)
               print(node.toxml())

"event" is one of the following constants, and "node" is the node
which the event is about. The nodes implement the "xml.dom"
interfaces; they are created by the DOM implementation given to
"PullDOM", which is "xml.dom.minidom" by default.

xml.dom.pulldom.START_DOCUMENT
xml.dom.pulldom.END_DOCUMENT

   The start and the end of the document. *node* is the "Document".

xml.dom.pulldom.START_ELEMENT
xml.dom.pulldom.END_ELEMENT

   The start tag and the end tag of an element. *node* is the
   "Element".

xml.dom.pulldom.CHARACTERS

   Character data. *node* is the "Text" node.

xml.dom.pulldom.IGNORABLE_WHITESPACE

   White space in element content, as declared in the DTD. *node* is
   the "Text" node.

xml.dom.pulldom.COMMENT

   A comment. *node* is the "Comment" node.

xml.dom.pulldom.PROCESSING_INSTRUCTION

   A processing instruction. *node* is the "ProcessingInstruction"
   node.

文書はイベントの *フラットな* 流れとして扱われるため、文書の "木" は暗
黙のうちに全て読み込まれ、目的の要素は木の中の深さに依らずに見つけられ
ます。つまり、文書ノードの再帰的な検索のような階層的な問題を考える必要
はありません。しかしながら要素の前後関係が重要な場合は、前後関係の状態
を維持する (すなわち文章中の任意の点の場所を記憶する) か、
"DOMEventStream.expandNode()" メソッドを使用して DOM 関連の処理に切り
替える必要があります。

class xml.dom.pulldom.PullDOM(documentFactory=None)

   Subclass of "xml.sax.handler.ContentHandler" which turns SAX events
   into the events of the pull parser. The nodes are created, but they
   are not added to the tree, unless "expandNode()" is called.
   *documentFactory*, if given, is a DOM implementation used to create
   the document; by default the implementation of "xml.dom.minidom" is
   used.

class xml.dom.pulldom.SAX2DOM(documentFactory=None)

   Subclass of "PullDOM" which also adds every created node to the
   tree, so that the complete document is built.

xml.dom.pulldom.parse(stream_or_string, parser=None, bufsize=None)

   与えられた入力から "DOMEventStream" を返します。*stream_or_string*
   はファイル名かファイル様オブジェクトのいずれかです。*parser* は、与
   えれた場合、 "XMLReader" オブジェクトでなければなりません。この関数
   はパーザの文書ハンドラを変えて名前空間のサポートを有効にします。パ
   ーザの他の設定 (例えばエンティティリゾルバ) は前もってしておかなけ
   ればなりません。

XML データを文字列で持っている場合、 "parseString()" を代わりに使うこ
とができます:

xml.dom.pulldom.parseString(string, parser=None)

   Return a "DOMEventStream" that represents the *string*. *string*
   must be a "str" instance; to parse bytes, pass a binary file object
   to "parse()".

xml.dom.pulldom.default_bufsize

   "parse()" の *bufsize* パラメタのデフォルト値です。

   この変数の値は "parse()" を呼び出す前に変更することができます。その
   場合、その新しい値が有効になります。


DOMEventStream オブジェクト
===========================

class xml.dom.pulldom.DOMEventStream(stream, parser, bufsize)

   Produce the events for the data read from the file object *stream*
   by the "XMLReader" *parser*. The data is read by *bufsize* bytes,
   or characters for a text stream, at a time.

   バージョン 3.11 で変更: "__getitem__()" メソッドのサポートは削除さ
   れました。

   getEvent()

      Return the next "(event, node)" tuple, or "None" at the end of
      the document. See above for the events and the corresponding
      nodes. The current node does not contain information about its
      children, unless "expandNode()" is called.

   expandNode(node)

      *node* の全子ノードを *node* に展開します。例:

         from xml.dom import pulldom

         xml = '<html><title>Foo</title> <p>Some text <div>and more</div></p> </html>'
         doc = pulldom.parseString(xml)
         for event, node in doc:
             if event == pulldom.START_ELEMENT and node.tagName == 'p':
                 # Following statement only prints '<p/>'
                 print(node.toxml())
                 doc.expandNode(node)
                 # Following statement prints node with all its children '<p>Some text <div>and more</div></p>'
                 print(node.toxml())

   reset()

      Discard the events which are not read yet and prepare the object
      for parsing a new document.

   clear()

      Release the parser and the document. The stream is not closed,
      and the object can no longer be used.
