phpxml_parse_into_struct() 是 PHP 内置的函数之一
▥php
𝄐 0
php xml转数组,php xml转json,php xml解析,php xml文件生成图片,php xml格式化,php xmpp
phpxml_parse_into_struct() 是 PHP 内置的函数之一,用于将 XML 字符串解析为结构化的数组。该函数的语法如下:
bool xml_parse_into_struct (
xmlparser $parser ,
string $data ,
array &$values [, array &$index ]
);
其中,$parser 参数是一个 XML 解析器对象,可以使用 xml_parser_create() 创建;$data 参数是需要解析的 XML 字符串;$values 参数用于存储解析后的结果,以数组形式返回;$index 参数(可选)用于存储每个元素在 $values 数组中的索引。
以下是一个示例代码,演示了如何使用 xml_parse_into_struct() 函数将 XML 字符串解析为数组:
php
$xml = '
<books>
<book>
<title>PHP Cookbook</title>
<author>Adam Trachtenberg</author>
<price>19.99</price>
</book>
<book>
<title>JavaScript: The Definitive Guide</title>
<author>David Flanagan</author>
<price>29.99</price>
</book>
</books>';
// 创建 XML 解析器
$parser = xml_parser_create();
// 解析 XML 字符串
xml_parse_into_struct($parser, $xml, $values);
// 输出解析后的数组
print_r($values);
// 关闭解析器
xml_parser_free($parser);
上述代码将输出以下内容:
Array
(
[0] => Array
(
[tag] => BOOKS
[type] => open
[level] => 1
)
[1] => Array
(
[tag] => BOOK
[type] => open
[level] => 2
)
[2] => Array
(
[tag] => TITLE
[type] => open
[level] => 3
)
[3] => Array
(
[tag] => TITLE
[type] => complete
[level] => 3
[value] => PHP Cookbook
)
[4] => Array
(
[tag] => AUTHOR
[type] => open
[level] => 3
)
[5] => Array
(
[tag] => AUTHOR
[type] => complete
[level] => 3
[value] => Adam Trachtenberg
)
[6] => Array
(
[tag] => PRICE
[type] => open
[level] => 3
)
[7] => Array
(
[tag] => PRICE
[type] => complete
[level] => 3
[value] => 19.99
)
[8] => Array
(
[tag] => BOOK
[type] => close
[level] => 2
)
[9] => Array
(
[tag] => BOOK
[type] => open
[level] => 2
)
[10] => Array
(
[tag] => TITLE
[type] => open
[level] => 3
)
[11] => Array
(
[tag] => TITLE
[type] => complete
[level] => 3
[value] => JavaScript: The Definitive Guide
)
[12] => Array
(
[tag] => AUTHOR
[type] => open
[level] => 3
)
[13] => Array
(
[tag] => AUTHOR
[type] => complete
[level] => 3
[value] => David Flanagan
)
[14] => Array
(
[tag] => PRICE
[type] => open
[level] => 3
)
[15] => Array
(
[tag] => PRICE
[type] => complete
[level] => 3
[value] => 29.99
)
[16] => Array
(
[tag] => BOOK
[type] => close
[level] => 2
)
[17] => Array
(
[tag]
phpxml_parse_into_struct() 是 PHP 内置的函数之一,用于将 XML 字符串解析为结构化的数组。该函数的语法如下:
bool xml_parse_into_struct (
xmlparser $parser ,
string $data ,
array &$values [, array &$index ]
);
其中,$parser 参数是一个 XML 解析器对象,可以使用 xml_parser_create() 创建;$data 参数是需要解析的 XML 字符串;$values 参数用于存储解析后的结果,以数组形式返回;$index 参数(可选)用于存储每个元素在 $values 数组中的索引。
以下是一个示例代码,演示了如何使用 xml_parse_into_struct() 函数将 XML 字符串解析为数组:
php
$xml = '
<books>
<book>
<title>PHP Cookbook</title>
<author>Adam Trachtenberg</author>
<price>19.99</price>
</book>
<book>
<title>JavaScript: The Definitive Guide</title>
<author>David Flanagan</author>
<price>29.99</price>
</book>
</books>';
// 创建 XML 解析器
$parser = xml_parser_create();
// 解析 XML 字符串
xml_parse_into_struct($parser, $xml, $values);
// 输出解析后的数组
print_r($values);
// 关闭解析器
xml_parser_free($parser);
上述代码将输出以下内容:
Array
(
[0] => Array
(
[tag] => BOOKS
[type] => open
[level] => 1
)
[1] => Array
(
[tag] => BOOK
[type] => open
[level] => 2
)
[2] => Array
(
[tag] => TITLE
[type] => open
[level] => 3
)
[3] => Array
(
[tag] => TITLE
[type] => complete
[level] => 3
[value] => PHP Cookbook
)
[4] => Array
(
[tag] => AUTHOR
[type] => open
[level] => 3
)
[5] => Array
(
[tag] => AUTHOR
[type] => complete
[level] => 3
[value] => Adam Trachtenberg
)
[6] => Array
(
[tag] => PRICE
[type] => open
[level] => 3
)
[7] => Array
(
[tag] => PRICE
[type] => complete
[level] => 3
[value] => 19.99
)
[8] => Array
(
[tag] => BOOK
[type] => close
[level] => 2
)
[9] => Array
(
[tag] => BOOK
[type] => open
[level] => 2
)
[10] => Array
(
[tag] => TITLE
[type] => open
[level] => 3
)
[11] => Array
(
[tag] => TITLE
[type] => complete
[level] => 3
[value] => JavaScript: The Definitive Guide
)
[12] => Array
(
[tag] => AUTHOR
[type] => open
[level] => 3
)
[13] => Array
(
[tag] => AUTHOR
[type] => complete
[level] => 3
[value] => David Flanagan
)
[14] => Array
(
[tag] => PRICE
[type] => open
[level] => 3
)
[15] => Array
(
[tag] => PRICE
[type] => complete
[level] => 3
[value] => 29.99
)
[16] => Array
(
[tag] => BOOK
[type] => close
[level] => 2
)
[17] => Array
(
[tag]
本文地址:
/show-270762.html
版权声明:除非特别标注原创,其它均来自互联网,转载时请以链接形式注明文章出处。