例 -- XML_Parser 使用の例

基本的な例

この基本的な例では、要素の開始と終了(つまり、開始タグと終了タグ)を 処理する単純なパーサを示します。


<?php
<?PHP
require_once 'XML/Parser.php';

class myParser extends XML_Parser
{
  function myParser()
  {
    parent::XML_Parser();
  }

 /**
  * 要素の開始の処理
  *
  * @access private
  * @param  resource  xml parser resource
  * @param  string    name of the element
  * @param  array     attributes
  */
  function startHandler($xp, $name, $attribs)
  {
    printf('handle start tag: %s<br />', $name);
  }

 /**
  * 要素の終了の処理
  *
  * @access private
  * @param  resource  xml parser resource
  * @param  string    name of the element
  */
  function endHandler($xp, $name)
  {
    printf('handle end tag: %s<br />', $name);
  }
}

$p = &new myParser();

$result = $p->setInputFile('xml_parser_file.xml');
$result = $p->parse();
?>
?>

このパーサは、単に、文書のパース中に見つかった開始タグと終了タグの 名前を出力するだけのものです。