PHP xml_parse_into_struct() 函数
PHP xml_parse_into_struct() 函数
完整的 PHP XML 参考手册定义和用法
xml_parse_into_struct() 函数把 XML 数据解析到数组中。
该函数把 XML 数据解析到 2 个数组中:
- Value 数组 - 包含来自被解析的 XML 的数据
- Index 数组 - 包含指向 Value 数组中值的位置的指针
如果成功,该函数则返回 1。如果失败,则返回 0。
语法
xml_parse_into_struct(parser,xml,value_arr,index_arr)
参数 | 描述 |
---|---|
parser | 必需。规定要使用的 XML 解析器。 |
xml | 必需。规定要解析的 XML 数据。 |
value_arr | 必需。规定 XML 数据的目标数组。 |
index_arr | 可选。规定 index 数据的目标数组。 |
提示和注释
注释:xml_parse_into_struct() 函数如果成功则返回 1,如果失败则返回 0。这与 TRUE 和 FALSE 不同,使用例如 === 运算符时要注意。
实例
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>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
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);
?>
//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);
?>
上面代码的输出如下所示:
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
)
)
(
[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
)
)
完整的 PHP XML 参考手册
相关文章
- PHP While 循环
- PHP For 循环
- PHP 魔术常量
- PHP Cookie
- PHP Secure E-mails
- PHP 过滤器
- PHP 高级过滤器
- PHP array() 函数
- PHP array_column() 函数
- PHP array_diff_assoc() 函数
- PHP array_intersect_key() 函数
- PHP array_sum() 函数
- PHP array_uintersect_uassoc() 函数
- PHP array_walk() 函数
- PHP current() 函数
- PHP natcasesort() 函数
- PHP next() 函数
- PHP reset() 函数
- PHP 5 Date/Time 函数
- PHP Filter 函数