php解析xml,并將xml轉(zhuǎn)換為層級(jí)數(shù)組

1)xml_parser_create([ string $encoding ] ):建立一個(gè)新的xml解析器并返回可被其他xml函數(shù)使用的資源句柄,

創(chuàng)新互聯(lián)公司專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)、望江網(wǎng)絡(luò)推廣、小程序設(shè)計(jì)、望江網(wǎng)絡(luò)營(yíng)銷、望江企業(yè)策劃、望江品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營(yíng)等,從售前售中售后,我們都將竭誠(chéng)為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);創(chuàng)新互聯(lián)公司為所有大學(xué)生創(chuàng)業(yè)者提供望江建站搭建服務(wù),24小時(shí)服務(wù)熱線:13518219792,官方網(wǎng)址:bm7419.com

參數(shù)$encoding:

php4,中用來只指定要被解析的xml輸入的字符編碼方式;

php5,自動(dòng)偵測(cè)輸入xml的編碼,encoding僅用來指定解析后輸出數(shù)據(jù)的編碼

默認(rèn):輸入編碼=輸出編碼

php5.0.2+默認(rèn)編碼utf-8;之前版本,ISO-8859-1

2)bool xml_parser_set_option(resource $parser,int $option,mixed $value):為指定的的xml解析進(jìn)行選項(xiàng)設(shè)置

parser:指向要設(shè)置選項(xiàng)信息的xml解析器指針

option:要設(shè)置選項(xiàng)的名稱

value:要設(shè)置選項(xiàng)的值

設(shè)置成功返回true,失敗返回false

選項(xiàng)數(shù)據(jù)類型描述

XML_OPTION_CASE_FOLDINGint 控制在該xml解析器中大小寫是否有效。默認(rèn)有效,0原樣輸出,1轉(zhuǎn)換為大寫,只控制輸出樣式

XML_OPTION_SKIP_TAGSTARTint 指明在一個(gè)標(biāo)記名前應(yīng)略過幾個(gè)字符

XML_OPTION_SKIP_WHITEint 是否略過由空白字符組成的值

XML_OPTION_TARGET_ENCODINGstring

3)int xml_parse_into_struct(resource $parser,string $data,array &$values [,array &$index]):將xml

文件解析到兩個(gè)對(duì)應(yīng)的數(shù)組中,index參數(shù)含有指向values數(shù)組中對(duì)應(yīng)值的指針,該函數(shù)返回的是一級(jí)數(shù)組,不想dom樹那樣有層級(jí)關(guān)系

失敗返回0,成功返回1

4)eg:

源文件:

<?xml version="1.0" encoding="utf-8"?>

<newdata>

   <version a="xxx">aaaa</version>

   <sample><![CDATA[0]]></sample>

   <all><![CDATA[https]]></all>

</newdata>

value結(jié)果:

Array

(

   [0] => Array

       (

       //標(biāo)簽名

           [tag] => newdata

           //節(jié)點(diǎn)狀態(tài),open:含有子標(biāo)簽,起始標(biāo)簽;close:open的閉合部分;complete:無子標(biāo)簽

           [type] => open

           //層級(jí)

           [level] => 1

       )

   [1] => Array

       (

           [tag] => version

           [type] => complete

           [level] => 2

           //節(jié)點(diǎn)屬性數(shù)組

           [attributes] => Array

               (

                   [a] => xxx

               )

           //節(jié)點(diǎn)值

           [value] => aaaa

       )

   [2] => Array

       (

           [tag] => sample

           [type] => complete

           [level] => 2

           [value] => 0

       )

   [3] => Array

       (

           [tag] => all

           [type] => complete

           [level] => 2

           [value] => https

       )

   [4] => Array

       (

           [tag] => newdata

           [type] => close

           [level] => 1

       )

)

索引結(jié)果:

Array

(

//節(jié)點(diǎn)名稱

   [newdata] => Array

       (

           [0] => 0//節(jié)點(diǎn)起始索引

           [1] => 4//節(jié)點(diǎn)結(jié)束索引

       )

   [version] => Array

       (

           [0] => 1

       )

   [sample] => Array

       (

           [0] => 2

       )

   [all] => Array

       (

           [0] => 3

       )

)

5)將xml轉(zhuǎn)換為array的函數(shù):

    

    /**

     * 將xml字符串轉(zhuǎn)換為數(shù)組

     * @param string $contents

     * @param string $encoding

     * @param int $get_attrbutes

     * @param string $priority

     * @param array

     */

    public static function xml2Array($contents = NULL, $encoding = 'UTF-8', $get_attributes = 1, $priority = 'tag') {

        if(!$contents) {

            return array();

        }

        if(!function_exists('xml_parser_create')) {

            return array();

        }

        //xml解析器

        $parser = xml_parser_create('');

        xml_parser_set_option($parser,XML_OPTION_TARGET_ENCODING,$encoding);

        //將標(biāo)簽原樣輸出,不轉(zhuǎn)換成大寫

        xml_parser_set_option($parser,XML_OPTION_CASE_FOLDING,0);

        //是否忽略空白字符

        xml_parser_set_option($parser,XML_OPTION_SKIP_WHITE,1);

        //$xml_values,$index引用類型,將文本解析到指定的數(shù)組變量中

        xml_parse_into_struct($parser, trim($contents), $xml_values/*,$index*/);

        //釋放解析器

        xml_parser_free($parser);

        if(!$xml_values)

            return array();

        $xml_array = array();

        $parents = array();

        $opened_tags = array();

        $arr = array();

        //當(dāng)前操作結(jié)構(gòu)的指針

        $current = & $xml_array;

        //同級(jí)結(jié)構(gòu)下重復(fù)標(biāo)簽的計(jì)數(shù)

        $repeated_tag_index = array();

        foreach ($xml_values as $data) {

            //刪除屬性和值,確保每次用到的是新的

            unset($attributes, $value);

            //將標(biāo)簽結(jié)構(gòu)數(shù)組,釋放到當(dāng)前的變量域中

            extract($data);

            //存當(dāng)前標(biāo)簽的結(jié)果

            $result = array();

            //存屬性

            $attributes_data = array();

            //標(biāo)簽有value

            if(isset($value)) {

                if($priority == 'tag'){

                   $result = trim($value); 

                }else{

                    $result['value'] = trim($value);

                }

            }

            //標(biāo)簽有屬性,且不忽略

            if($get_attributes && isset($attributes)) {

                foreach ($attributes as $attr => $val) {

                    if ($priority == 'tag'){//放入單獨(dú)記錄屬性的數(shù)組中

                        $attributes_data[$attr] = $val;

                    }else{//統(tǒng)一放入$result中

                        $result['attr'][$attr] = $val;

                    }

                }

            }

            //處理節(jié)點(diǎn)關(guān)系

            if ($type == "open") {//有子節(jié)點(diǎn)標(biāo)簽

                $parent[$level - 1] = & $current; //$parent[$level - 1],指向復(fù)合標(biāo)簽的起始處

                if (!is_array($current) || (!in_array($tag, array_keys($current)))) {//xml復(fù)合標(biāo)簽的第一個(gè)

                    $current[$tag] = $result;//屬性獨(dú)立

                    /*處理結(jié)果

                        [tag] => Array

                        (

                            [value] => aaaa,

                            [attr] => Array

                                (

                                    [a] => xxx

                                )

                        )

                    */

                    if ($attributes_data){

                        $current[$tag . '_attr'] = $attributes_data;

                        /*處理結(jié)果

                            [tag] => xxxx,

                            [tag_attr] => Array

                                (

                                    [a] => xxx

                                )

                        */

                    }

                    $repeated_tag_index[$tag . '_' . $level] = 1;//記錄同級(jí)中該標(biāo)簽重復(fù)的個(gè)數(shù)

                    //指針重新指向符合標(biāo)簽的子標(biāo)簽

                    $current = & $current[$tag];

                }else {

                    if (isset($current[$tag][0])) {//第3+個(gè)同級(jí)復(fù)合標(biāo)簽

                        $current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result;

                        $repeated_tag_index[$tag . '_' . $level] ++;

                    } else {//第2個(gè)同級(jí)復(fù)合標(biāo)簽

                        //在關(guān)聯(lián)數(shù)組外包一層索引數(shù)組

                        $current[$tag] = array(

                            $current[$tag],

                            $result

                        );

                        $repeated_tag_index[$tag . '_' . $level] = 2;

                        //此處只記錄第一個(gè)重復(fù)標(biāo)簽的屬性,可能有bug,需注意!

                        //要想?yún)^(qū)別各子標(biāo)簽的屬性,需要將$priority設(shè)成非'tag'

                        if (isset($current[$tag . '_attr'])) {

                            $current[$tag]['0_attr'] = $current[$tag . '_attr'];

                            unset($current[$tag . '_attr']);

                        }

                    }

                    //記錄最后一個(gè)重復(fù)子標(biāo)簽的索引

                    $last_item_index = $repeated_tag_index[$tag . '_' . $level] - 1;

                    //指針指向下一個(gè)子標(biāo)簽

                    $current = & $current[$tag][$last_item_index];

                }

            } elseif ($type == "complete") {

                //第一個(gè)complete類型的標(biāo)簽

                if (!isset($current[$tag])) {

                    $current[$tag] = $result;

                    $repeated_tag_index[$tag . '_' . $level] = 1;

                    if ($priority == 'tag' && $attributes_data)

                        $current[$tag . '_attr'] = $attributes_data;

                }

                else {

                    //第3+個(gè)同級(jí)子標(biāo)簽

                    //此處只有$current[$tag][0],不行,因?yàn)榭赡芩饕阶址牡谝粋€(gè)字符

                    if(isset($current[$tag][0]) && !is_array($current[$tag])){

                        print_r($current);exit();

                    }

                    if(isset($current[$tag][0]) && is_array($current[$tag])) {

                        $current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result;

                        //子標(biāo)簽的屬性不忽略

                        if ($get_attributes &&  $priority == 'tag' && $attributes_data) {

                            $current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data;

                        }

                        $repeated_tag_index[$tag . '_' . $level] ++;

                    }else{//第2個(gè)同級(jí)子標(biāo)簽

                        $current[$tag] = array(

                            $current[$tag],

                            $result

                        );

                        $repeated_tag_index[$tag . '_' . $level] = 1;

                        if ($priority == 'tag' && $get_attributes) {

                            if (isset($current[$tag . '_attr'])) {

                                $current[$tag]['0_attr'] = $current[$tag . '_attr'];

                                unset($current[$tag . '_attr']);

                            }

                            if ($attributes_data) {

                                $current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data;

                            }

                        }

                        $repeated_tag_index[$tag . '_' . $level] ++;

                    }

                }

            }elseif($type == 'close'){

                //閉合標(biāo)簽和起始標(biāo)簽level相同,因此進(jìn)入complete類型的子標(biāo)簽后,可以通過父節(jié)點(diǎn)的close標(biāo)簽,可以指回到父節(jié)點(diǎn)

                $current = & $parent[$level - 1];

            }

        }

        return $xml_array;

    }

6)關(guān)于xml中的CDATA:

    在XML文檔中的所有文本都會(huì)被解析器解析。只有在CDATA部件之內(nèi)的文本會(huì)被解析器忽略

    一個(gè) CDATA 部件以"< ![CDATA[" 標(biāo)記開始,以"]]>"標(biāo)記結(jié)束:

    < script>
    < ![CDATA[
    function matchwo(a,b)
    {
        if (a < b && a < 0) then
        {
            return 1
        }
        else
        {
            return 0
        }
      }
     ]]>
    < /script>

    在前面的例子中,所有在CDATA部件之間的文本都會(huì)被解析器忽略。

    CDATA注意事項(xiàng):
    CDATA部件之間不能再包含CDATA部件(不能嵌套)。如果CDATA部件包含了字符"]]>" 或者"< !        [CDATA[" ,將很有可能出錯(cuò)哦。

    同樣要注意在字符串"]]>"之間沒有空格或者換行符

參考地址:http://www.cnblogs.com/chenqingwei/archive/2010/04/21/1717237.html

網(wǎng)頁題目:php解析xml,并將xml轉(zhuǎn)換為層級(jí)數(shù)組
分享路徑:http://bm7419.com/article44/pcgdhe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號(hào)、網(wǎng)站收錄網(wǎng)站改版、品牌網(wǎng)站設(shè)計(jì)、關(guān)鍵詞優(yōu)化、品牌網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站建設(shè)