返回列表 发帖

一个操作xml的PHP类(附实例)

本帖最后由 gotophp 于 2009-7-1 11:24 编辑
  1. <?php
  2. /**
  3. * @copyright 互联网
  4. * 整理:[url]www.phppx.com[/url]
  5. * @desc 一个操作xml的PHP类
  6. */

  7. /*
  8.    (c) 2000 Hans Anderson Corporation.  All Rights Reserved.
  9.    You are free to use and modify this class under the same
  10.    guidelines found in the PHP License.

  11.    -----------

  12.    bugs/me:
  13.    [url]http://www.hansanderson.com/php/[/url]
  14.    [email]me@hansanderson.com[/email]
  15.    [email]showstv@163.com[/email]

  16.    -----------

  17.    Version 1.0

  18.       - 1.0 is the first actual release of the class.  It"s  
  19.        finally what I was hoping it would be, though there
  20.        are likely to still be some bugs in it.  This is
  21.        a much changed version, and if you have downloaded
  22.        a previous version, this WON"T work with your existing
  23.        scripts!  You"ll need to make some SIMPLE changes.

  24.       - .92 fixed bug that didn"t include tag attributes

  25.        (to use attributes, add _attributes[array_index]
  26.         to the end of the tag in question:
  27.         $xml_html_head_body_img would become
  28.         $xml_html_head_body_img_attributes[0],  
  29.         for example)

  30.         -- Thanks to Nick Winfield <[email]nick@wirestation.co.uk[/email]>
  31.           for reporting this bug.

  32.       - .91 No Longer requires PHP4!

  33.       - .91 now all elements are array.  Using objects has
  34.        been discontinued.
  35. */

  36. class xml_container{

  37.    function store($k,$v) {
  38.       $this->{$k}[] = $v;
  39.    }
  40. }


  41. /* parses the information */
  42. /*********************************
  43. *   类定义开始
  44. *
  45. *********************************/
  46. class xml{
  47.    
  48.    // initialize some variables
  49.    var $current_tag=array();
  50.    var $xml_parser;
  51.    var $Version = 1.0;
  52.    var $tagtracker = array();

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


  54.    /* when expat hits an opening tag, it fires up this function */
  55.    function startElement($parser, $name, $attrs){

  56.       array_push($this->current_tag, $name); // add tag to the cur. tag array
  57.       $curtag = implode("_",$this->current_tag); // piece together tag

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

  59.       if(isset($this->tagtracker["$curtag"])) {
  60.         $this->tagtracker["$curtag"]++;
  61.       }
  62.       else{
  63.         $this->tagtracker["$curtag"]=0;
  64.       }

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

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

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

  72.         $GLOBALS[$this->identifier]["$curtag"][$j]->store("attributes",$attrs);
  73.       }
  74.    
  75.    }// end function startElement



  76.    /* when expat hits a closing tag, it fires up this function */
  77.    function endElement($parser, $name) {
  78.       $curtag = implode("_",$this->current_tag); // piece together tag
  79.       
  80.       // before we pop it off,
  81.       // so we can get the correct
  82.       // cdata

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

  91.       $popped = array_pop($this->current_tag);
  92.       // we want the tag name for
  93.       // the tag above this, it  
  94.       // allows us to group the
  95.       // tags together in a more
  96.       // intuitive way.

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

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

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

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

  106.       $GLOBALS[$this->identifier]["$curtag"][$j]->store($name,$TD);
  107.       #$this->tagdata["$curtag"]);
  108.       unset($TD);
  109.       return TRUE;
  110.    } // end function endElement


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

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


  117.    function xml($data,$identifier="xml") {  

  118.       $this->identifier = $identifier;

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

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

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

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

  133.    }//end constructor: function xml()


  134. }//thus, we end our class xml

  135. ?>
复制代码
操作方法:
  1. <?php
  2. require("class.xml.php");
  3. $file = "data.xml";
  4. $data = implode("",file($file)) or die("could not open XML input file");
  5. $obj = new xml($data,"xml");


  6. print $xml["hans"][0]->num_results[0];
  7. for($i=0;$i<sizeof($xml["hans"]);$i++) {
  8.         print $xml["hans"][$i]->tag[0] . " ";
  9. }

  10. //To print url attributes (if they exist):

  11. print $xml["hans"][0]->attributes[0]["size"];

  12. ?>
复制代码

回复......

此帖要顶,按个爪印防止忘了。。。


















美国主机|fatcow主机|lunarpages主机|IXwebhosting主机

TOP

返回列表