标签:php xml xml_parse_into_struc
函数原型: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. 标签名字,例如<bookname>parse xml sources</bookname>,则标签名字为:bookname
2. 标签所处状态(或者说是类型),<bookname>parse xml sources</bookname>,当解析器读取到<bookname>,则标签类型为:open(开启状态),当解析器读取到</bookname>时,则标签类型为:close(闭合状态)
3. 当前元素所处XML解析数的第几层(XML通常被解析为一颗倒置树,根(顶层元素)处于第一层 )比如 <book><bookname>parse xml sources</bookname><authors>aaaa, bbbb</authors></book>中,book标签所标记的元素处于解析数的第一层,即level为1, bookname和authors标签所标记的元素都处于解析数的第二层,即level为2
4. 可选的值。在例子<book><bookname>parse xml sources</bookname><authors>aaaa, bbbb</authors></book>中,bookname标签所标记的取值为字符串 " parse xml sources" ,authors标签所标记的取值为字符串" aaaa,bbbb" 。 而book标签所标记的元素没有直接的字符结点所以取值为空(或者NULL)
@param-->&$index 解析完成后生成的对应数组$value中元素取值的索引数组,从 0 开始统计。比如在 <book><bookname>parse xml sources</bookname><authors>aaaa, bbbb</authors></book>中,<book>所代表的其实标签处于索引数组中的0位置,<bookname>parse xml sources</bookname>所代表的结点处于索引数组中的位置为1, <authors>aaaa, bbbb</authors>所代表的结点在索引数组中的位置为2,</book>所代表的闭合标签所处的位置则为3, 所以book所代表的取值范围为book{0,3},bookname的取值范围为bookname{1}, authors的取值范围为authors{2}
举例:
<?php $xml=<<<XML <?xml version="1.0"?> <moldb> <molecule> <name>Alanine</name> <symbol>ala</symbol> <code>A</code> <type>hydrophobic</type> </molecule> <molecule> <name>Lysine</name> <symbol>lys</symbol> <code>K</code> <type>charged</type> </molecule> </moldb> 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 "<pre>"; print_r($val); echo "<br />"; print_r($index); echo "</pre>"; ?>
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 ) )
<moldb><--0--> <molecule><--1--> <name>Alanine</name><--2--> <symbol>ala</symbol><--3--> <code>A</code><--4--> <type>hydrophobic</type><--5--> </molecule><--6--> <molecule><--7--> <name>Lysine</name><--8--> <symbol>lys</symbol><--9--> <code>K</code><--10--> <type>charged</type><--11--> </molecule><--12--> </moldb><--13-->
PHP xml_parse_into_struct 函数详细解析
标签:php xml xml_parse_into_struc
原文地址:http://blog.csdn.net/free_program_1314/article/details/38922639