搜尋
首頁php教程php手册今天看到一个很好的类-操作xml的!贴出来给大家,不知道以前贴过没有?

xml


/*
    (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[0],  
           for example)

           -- Thanks to Nick Winfield
              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->{$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->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;
    } // end function endElement


    /* 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;
    }


    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

?>




操作方法:

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"][0]->num_results[0];
for($i=0;$i print $xml["hans"][$i]->tag[0] . " ";
}

To print url attributes (if they exist):

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



陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
4 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SecLists

SecLists

SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具