当前位置: 首页 > 网络学院 > 服务端脚本教程 > PHP > xml_parse_into_struct() 函数
The xml_parse_into_struct() function parses XML data into an array.
xml_parse_into_struct()函数的作用是:将XML数据解析到一个数组中。
This function parses the XML data into 2 arrays:
这个函数将XML数据解析成2个数组:
This function returns 1 on success, or 0 on failure.
如果函数成功执行,将返回1;如果执行失败,将返回0。
xml_parse_into_struct(parser,xml,value_arr,index_arr) |
Parameter 参数 | Description 描述 |
---|---|
parser | Required. Specifies XML parser to use 必要参数。指定需要解析的XML |
xml | Required. Specifies XML data to parse 必要参数。指定需要解析的XML数据 |
value_arr | Required. Specifies the target array for the XML data 必要参数。指定XML数据的目标数组[the target array for the XML data] |
index_arr | Optional. Specifies the target array for index data 可选参数。指定索引数据的目标数组[the target array for index data] |
Note: The xml_parse_into_struct() function returns 1 for success and 0 for failure. This is not the same as TRUE and FALSE.
注意:xml_parse_into_struct()函数如果成功执行,将返回1(不是True);如果执行失败,将返回0(不是False)。
XML File
XML文件
<?xml version="1.0" encoding="ISO-8859-1"?> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note> |
PHP Code
PHP 代码
<?php //invalid xml file $xmlfile = 'test.xml'; $xmlparser = xml_parser_create(); // open a file and read data $fp = fopen($xmlfile, 'r'); $xmldata = fread($fp, 4096); xml_parse_into_struct($xmlparser,$xmldata,$values); xml_parser_free($xmlparser); print_r($values); ?> |
The output of the code above will be:
上述代码将输出下面的结果:
Array ( [0] => Array ( [tag] => NOTE [type] => open [level] => 1 [value] => ) [1] => Array ( [tag] => TO [type] => complete [level] => 2 [value] => Tove ) [2] => Array ( [tag] => NOTE [value] => [type] => cdata [level] => 1 ) [3] => Array ( [tag] => FROM [type] => complete [level] => 2 [value] => Jani ) [4] => Array ( [tag] => NOTE [value] => [type] => cdata [level] => 1 ) [5] => Array ( [tag] => HEADING [type] => complete [level] => 2 [value] => Reminder ) [6] => Array ( [tag] => NOTE [value] => [type] => cdata [level] => 1 ) [7] => Array ( [tag] => BODY [type] => complete [level] => 2 [value] => Don't forget me this weekend! ) [8] => Array ( [tag] => NOTE [value] => [type] => cdata [level] => 1 ) [9] => Array ( [tag] => NOTE [type] => close [level] => 1 ) ) |