ホームページ  >  記事  >  php教程  >  PHP のクラス - XML の操作(3)

PHP のクラス - XML の操作(3)

WBOY
WBOYオリジナル
2016-06-21 09:02:00819ブラウズ

/* expat に必要な XML 関数は次のとおりです */


/* expat が開始タグにヒットすると、この関数が起動されます */

function startElement($parser, $name, $attrs) {

array_push($this->current_tag, $name); // cur にタグを追加します。タグ配列

$curtag = implode("_",$this->current_tag); // タグ

/* これは、このタグの配列インデックスを追跡します */

if(isset($this-> tagtracker["$curtag"])) {
$this->tagtracker["$curtag"]++;
} else {
$this->tagtracker["$curtag"]=0;
}


/* このタグに属性がある場合は、ここで設定します。 */

if(count($attrs)>0) {
$j = $this->tagtracker["$ curtag"];
if(!$j) $j = 0;

if(!is_object($GLOBALS[$this->identifier]["$curtag"][$j])) {
$ GLOBALS[$this->identifier]["$curtag"][$j] = new xml_container;
}

$GLOBALS[$this->identifier]["$curtag"][$j]->store("attributes",$attrs );
}

} // 関数終了 startElement



/* expat が終了タグにヒットすると、この関数を起動します */

function endElement($parser, $name) {

$curtag = implode("_",$ this->current_tag);     // タグ
// 取り出す前に、
// これにより、正しい
// cdata

if(!$this ->tagdata["$curtag"]) {
$popped = array_pop($this->>current_tag ); // そうしないと、今いる場所で失敗してしまいます
return;     // タグ
} else {
$TD = $this->tagdata["$curtag"];
unset($this->tagdata["$curtag"]);
}

$popped = array_pop($this->current_tag);
//
// この上のタグ、
//
// タグをさらに
にグループ化できます。                               // intuitive way.

       if(sizeof($this->current_tag) == 0) return;     // if we aren't in a tag

       $curtag = implode("_",$this->current_tag);     // piece together tag
                               // this time for the arrays

       $j = $this->tagtracker["$curtag"];
       if(!$j) $j = 0;

       if(!is_object($GLOBALS[$this->identifier]["$curtag"][$j])) {
           $GLOBALS[$this->identifier]["$curtag"][$j] = new xml_container;
       }

       $GLOBALS[$this->identifier]["$curtag"][$j]->store($name,$TD); #$this->tagdata["$curtag"]);
       unset($TD);
       return TRUE;
   }



   /* when expat finds some internal tag character data,
      it fires up this function */

   function characterData($parser, $cdata) {
       $curtag = implode("_",$this->current_tag); // piece together tag        
       $this->tagdata["$curtag"] .= $cdata;
   }


   /* this is the constructor: automatically called when the class is initialized */

   function xml($data,$identifier='xml') {  

       $this->identifier = $identifier;

       // create parser object
       $this->xml_parser = xml_parser_create();

       // set up some options and handlers
       xml_set_object($this->xml_parser,$this);
       xml_parser_set_option($this->xml_parser,XML_OPTION_CASE_FOLDING,0);
       xml_set_element_handler($this->xml_parser, "startElement", "endElement");
       xml_set_character_data_handler($this->xml_parser, "characterData");

       if (!xml_parse($this->xml_parser, $data, TRUE)) {
           sprintf("XML error: %s at line %d",
           xml_error_string(xml_get_error_code($this->xml_parser)),
           xml_get_current_line_number($this->xml_parser));
       }

       // we are done with the parser, so let's free it
       xml_parser_free($this->xml_parser);

 

   }  // end constructor: function xml()


} // thus, we end our class xml

?>

 



声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。