您现在的位置是:首页-> 米鼠技术 ->今天看到一个很好的类-操作xml的!

今天看到一个很好的类-操作xml的!

<font size="3">&lt;</font>?
/*
    (c) 2000 Hans Anderson Corporation.  All Rights Reserved.
    You are free to use and modify this class under the same
    guidelines found in the PHP License.

    -----------

    bugs/me:
    http://www.hansanderson.com/php/
    me@hansanderson.com
    showstv@163.com

    -----------

    Version 1.0

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

        - .92 fixed bug that didn't include tag attributes

          (to use attributes, add _attributes[array_index]
           to the end of the tag in question:
            $xml_html_head_body_img would become
            $xml_html_head_body_img_attributes,  
           for example)

           -- Thanks to Nick Winfield
<font size="3">&lt;</font>nick@wirestation.co.uk<font size="3">&gt;</font>
              for reporting this bug.

        - .91 No Longer requires PHP4!

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

class xml_container{

    function store($k,$v) {
        $this-
<font size="3">&gt;</font>{$k} = $v;
    }
}


/* parses the information */
/*********************************
*    类定义开始
*
*********************************/
class xml{
    
    // initialize some variables
    var $current_tag=array();
    var $xml_parser;
    var $Version = 1.0;
    var $tagtracker = array();

    /* 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-
<font size="3">&gt;</font>current_tag, $name); // add tag to the cur. tag array
        $curtag = implode("_",$this-
<font size="3">&gt;</font>current_tag); // piece together tag

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

        if(isset($this-
<font size="3">&gt;</font>tagtracker["$curtag"])) {
            $this-
<font size="3">&gt;</font>tagtracker["$curtag"]++;
        }
        else{
            $this-
<font size="3">&gt;</font>tagtracker["$curtag"]=0;
        }

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

        if(count($attrs)
<font size="3">&gt;</font>0) {
            $j = $this-
<font size="3">&gt;</font>tagtracker["$curtag"];
            if(!$j) $j = 0;

            if(!is_object($GLOBALS[$this-
<font size="3">&gt;</font>identifier]["$curtag"][$j])) {
                $GLOBALS[$this-
<font size="3">&gt;</font>identifier]["$curtag"][$j] = new xml_container;
            }

            $GLOBALS[$this-
<font size="3">&gt;</font>identifier]["$curtag"][$j]-<font size="3">&gt;</font>store("attributes",$attrs);
        }
    
    }// end function startElement



    /* when expat hits a closing tag, it fires up this function */
    function endElement($parser, $name) {
        $curtag = implode("_",$this-
<font size="3">&gt;</font>current_tag); // piece together tag
        
        // before we pop it off,
        // so we can get the correct
        // cdata

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

        $popped = array_pop($this-
<font size="3">&gt;</font>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-
<font size="3">&gt;</font>current_tag) == 0) return; // if we aren't in a tag

        $curtag = implode("_",$this-
<font size="3">&gt;</font>current_tag); // piece together tag
        // this time for the arrays

        $j = $this-
<font size="3">&gt;</font>tagtracker["$curtag"];
        
        if(!$j) $j = 0;

        if(!is_object($GLOBALS[$this-
<font size="3">&gt;</font>identifier]["$curtag"][$j])) {
            $GLOBALS[$this-
<font size="3">&gt;</font>identifier]["$curtag"][$j] = new xml_container;
        }

        $GLOBALS[$this-
<font size="3">&gt;</font>identifier]["$curtag"][$j]-<font size="3">&gt;</font>store($name,$TD);
        #$this-
<font size="3">&gt;</font>tagdata["$curtag"]);
        unset($TD);
        return TRUE;
    } // end function endElement


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

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


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

        $this-
<font size="3">&gt;</font>identifier = $identifier;

        // create parser object
        $this-
<font size="3">&gt;</font>xml_parser = xml_parser_create();

        // set up some options and handlers
        xml_set_object($this-
<font size="3">&gt;</font>xml_parser,$this);
        xml_parser_set_option($this-
<font size="3">&gt;</font>xml_parser,XML_OPTION_CASE_FOLDING,0);
        xml_set_element_handler($this-
<font size="3">&gt;</font>xml_parser, "startElement", "endElement");
        xml_set_character_data_handler($this-
<font size="3">&gt;</font>xml_parser, "characterData");

        if (!xml_parse($this-
<font size="3">&gt;</font>xml_parser, $data, TRUE)) {
            sprintf("XML error: %s at line %d",
            xml_error_string(xml_get_error_code($this-
<font size="3">&gt;</font>xml_parser)),
            xml_get_current_line_number($this-
<font size="3">&gt;</font>xml_parser));
        }

        // we are done with the parser, so let's free it
        xml_parser_free($this-
<font size="3">&gt;</font>xml_parser);

    }//end constructor: function xml()


}//thus, we end our class xml

?
<font size="3">&gt;</font>




操作方法

require('class.xml.php');
$file = "data.xml";
$data = implode("",file($file)) or die("could not open XML input file");
$obj = new xml($data,"xml");


print $xml["hans"]-
<font size="3">&gt;</font>num_results;
for($i=0;$i
<font size="3">&lt;</font>sizeof($xml["hans"]);$i++) {
print $xml["hans"][$i]-
<font size="3">&gt;</font>tag . " ";
}

To print url attributes (if they exist):

print $xml["hans"]-
<font size="3">&gt;</font>attributes["size"];


热点文章
最新项目
相关文章 最新文章