PHP中的类-操作XML(3)

来源:互联网 发布:w3m linux 编辑:程序博客网 时间:2024/06/05 02:56
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

XML:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /> 

/* Here are the XML functions needed by expat */


   /* when expat hits an opening tag, it fires up this function */

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

       array_push($this->current_tag, $name); // add tag to the cur. tag array

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

       /* this tracks what array index we are on for this tag */

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


       /* if there are attributes for this tag, we set them here. */

       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);
               }

   } // end function startElement



   /* when expat hits a closing tag, it fires up this function */

   function endElement($parser, $name) {

       $curtag = implode("_",$this->current_tag);     // piece together tag
                               // before we pop it off,
                               // so we can get the correct
                               // cdata

       if(!$this->tagdata["$curtag"]) {
           $popped = array_pop($this->current_tag); // or else we screw up where we are
           return;     // if we have no data for the tag
       } else {
           $TD = $this->tagdata["$curtag"];
           unset($this->tagdata["$curtag"]);
       }

       $popped = array_pop($this->current_tag);
                               // we want the tag name for
                               // the tag above this, it  
                               // allows us to group the
                               // tags together in a more
                               // 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

?>

 <script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>