
本文旨在提供一种使用PHP解析包含CDATA的XML数据,并将其转换为数组的方法。我们将重点介绍如何使用SimpleXML处理XML结构,以及如何通过循环和JSON编码解码来提取所需的数据,包括属性和文本内容。通过本文,你将学会如何从复杂的XML结构中提取并组织数据,以便在PHP应用程序中使用。
在PHP中,SimpleXML 扩展提供了一种简单的方式来处理XML文档。然而,当XML结构较为复杂,特别是包含嵌套的数组和属性时,直接使用 SimpleXML 可能不够方便。以下是一种将 XML 数据转换为更易于操作的数组的常见方法。
假设我们有以下XML数据:
<Question type="2" text="Which one of the following area codes is associated with you?">
<Answer correct="false">606</Answer>
<Answer correct="false">859</Answer>
<Answer correct="false">616</Answer>
<Answer correct="false">614/380</Answer>
<Answer correct="false">812</Answer>
<Answer correct="true">502</Answer>
<Answer correct="false">810</Answer>
<Answer correct="false">740</Answer>
<Answer correct="false">248</Answer>
<Answer correct="false">None of the above</Answer>
</Question>以下PHP代码演示了如何解析这段XML数据并将其转换为数组:
<?php
$response = '<Question type="2" text="Which one of the following area codes is associated with you?">
<Answer correct="false">606</Answer>
<Answer correct="false">859</Answer>
<Answer correct="false">616</Answer>
<Answer correct="false">614/380</Answer>
<Answer correct="false">812</Answer>
<Answer correct="true">502</Answer>
<Answer correct="false">810</Answer>
<Answer correct="false">740</Answer>
<Answer correct="false">248</Answer>
<Answer correct="false">None of the above</Answer>
</Question>';
$objXmlDocument = simplexml_load_string($response, null, LIBXML_NOCDATA);
if ($objXmlDocument === false) {
echo "There were errors parsing the XML file.\n";
foreach (libxml_get_errors() as $error) {
echo $error->message;
}
exit;
}
$arrOutput = json_decode(json_encode($objXmlDocument), true);
unset($arrOutput['Answer']);
foreach ($objXmlDocument as $key => $answer) {
$arrOutput[$key][] = json_decode(json_encode($answer), true);
}
echo var_export($arrOutput, true) . PHP_EOL;
?>以上代码将输出以下数组结构:
array (
'@attributes' =>
array (
'type' => '2',
'text' => 'Which one of the following area codes is associated with you?',
),
'Answer' =>
array (
0 =>
array (
'@attributes' =>
array (
'correct' => 'false',
),
0 => '606',
),
1 =>
array (
'@attributes' =>
array (
'correct' => 'false',
),
0 => '859',
),
2 =>
array (
'@attributes' =>
array (
'correct' => 'false',
),
0 => '616',
),
3 =>
array (
'@attributes' =>
array (
'correct' => 'false',
),
0 => '614/380',
),
4 =>
array (
'@attributes' =>
array (
'correct' => 'false',
),
0 => '812',
),
5 =>
array (
'@attributes' =>
array (
'correct' => 'true',
),
0 => '502',
),
6 =>
array (
'@attributes' =>
array (
'correct' => 'false',
),
0 => '810',
),
7 =>
array (
'@attributes' =>
array (
'correct' => 'false',
),
0 => '740',
),
8 =>
array (
'@attributes' =>
array (
'correct' => 'false',
),
0 => '248',
),
9 =>
array (
'@attributes' =>
array (
'correct' => 'false',
),
0 => 'None of the above',
),
),
)通过使用 SimpleXML 结合 JSON 编码解码,可以方便地将包含 CDATA 的 XML 数据转换为 PHP 数组。这种方法尤其适用于处理复杂的 XML 结构,并从中提取所需的数据。然而,在实际应用中,需要根据 XML 的具体结构和应用程序的需求,选择最合适的解析方法。
以上就是解析包含CDATA的XML数组数据的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号