PHP xml_parse_into_struct 函數詳細解析

來源:互聯網
上載者:User
函數原型:int xml_parse_into_struct ( resource $parser , string $data , array &$values [, array &$index ] )

參數說明:@param-->$parser XML解析器,由xml_parser_create()產生一個XML資源控制代碼。

@param-->$data 帶解析的XML字串。

@param--> &$value 解析完成後產生的資料數組。

通常包括:1. 標籤名字,例如parse xml sources,則標籤名字為:bookname

2. 標籤所處狀態(或者說是類型),parse xml sources,當解析器讀取到,則標籤類型為:open(開啟狀態),當解析器讀取到時,則標籤類型為:close(閉合狀態)

3. 當前元素所處XML解析數的第幾層(XML通常被解析為一顆倒置樹,根(頂層元素)處於第一層 )比如 parse xml sourcesaaaa, bbbb中,book標籤所標記的元素處於解析數的第一層,即level為1, bookname和authors標籤所標記的元素都處於解析數的第二層,即level為2

4. 可選的值。在例子parse xml sourcesaaaa, bbbb中,bookname標籤所標記的取值為字串 " parse xml sources" ,authors標籤所標記的取值為字串" aaaa,bbbb" 。 而book標籤所標記的元素沒有直接的字元結點所以取值為空白(或者NULL)

@param-->&$index 解析完成後產生的對應數組$value中元素取值的索引數組,從 0 開始統計。比如在 parse xml sourcesaaaa, bbbb中,所代表的其實標籤處於索引數組中的0位置,parse xml sources所代表的結點處於索引數組中的位置為1, aaaa, bbbb所代表的結點在索引數組中的位置為2,所代表的閉合標籤所處的位置則為3, 所以book所代表的取值範圍為book{0,3},bookname的取值範圍為bookname{1}, authors的取值範圍為authors{2}


舉例:


        Alanine        ala        A        hydrophobic            Lysine        lys        K        charged    XML;$parse = xml_parser_create();xml_parser_set_option($parse, XML_OPTION_CASE_FOLDING, 1);xml_parser_set_option($parse, XML_OPTION_SKIP_WHITE, 1);$val = array();$index = array();xml_parse_into_struct($parse, $xml, $val, $index);echo "
";print_r($val);echo "
";print_r($index);echo "
";?>


解析結果
Array(    [0] => Array        (            [tag] => MOLDB            [type] => open            [level] => 1        )    [1] => Array        (            [tag] => MOLECULE            [type] => open            [level] => 2        )    [2] => Array        (            [tag] => NAME            [type] => complete            [level] => 3            [value] => Alanine        )    [3] => Array        (            [tag] => SYMBOL            [type] => complete            [level] => 3            [value] => ala        )    [4] => Array        (            [tag] => CODE            [type] => complete            [level] => 3            [value] => A        )    [5] => Array        (            [tag] => TYPE            [type] => complete            [level] => 3            [value] => hydrophobic        )    [6] => Array        (            [tag] => MOLECULE            [type] => close            [level] => 2        )    [7] => Array        (            [tag] => MOLECULE            [type] => open            [level] => 2        )    [8] => Array        (            [tag] => NAME            [type] => complete            [level] => 3            [value] => Lysine        )    [9] => Array        (            [tag] => SYMBOL            [type] => complete            [level] => 3            [value] => lys        )    [10] => Array        (            [tag] => CODE            [type] => complete            [level] => 3            [value] => K        )    [11] => Array        (            [tag] => TYPE            [type] => complete            [level] => 3            [value] => charged        )    [12] => Array        (            [tag] => MOLECULE            [type] => close            [level] => 2        )    [13] => Array        (            [tag] => MOLDB            [type] => close            [level] => 1        ))Array(    [MOLDB] => Array        (            [0] => 0            [1] => 13        )    [MOLECULE] => Array        (            [0] => 1            [1] => 6            [2] => 7            [3] => 12        )    [NAME] => Array        (            [0] => 2            [1] => 8        )    [SYMBOL] => Array        (            [0] => 3            [1] => 9        )    [CODE] => Array        (            [0] => 4            [1] => 10        )    [TYPE] => Array        (            [0] => 5            [1] => 11        ))

下面對$index索引數組做進一步分析,比如在上面的例子中(<--此處表示的是注釋,用語解釋說明,表示在解析數組中,他們所處的位置。 同樣我們需要擷取他們的值得時候,就根據這個索引來取值-->):


<--0--><--1-->        Alanine<--2-->        ala<--3-->        A<--4-->        hydrophobic<--5-->    <--6--><--7-->        Lysine<--8-->        lys<--9-->        K<--10-->        charged<--11-->    <--12--><--13-->

比如上例XML中有2個molecule元素,第一個為molecule{1,6},第2個為molecule{7,12}。 同樣有兩個name元素,第一個為name{2},第2個為name{8},以此類推。
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.