
本文旨在提供一种使用PHP解析包含CDATA的XML数据,并将其转换为数组的方法。我们将重点介绍如何使用SimpleXML处理XML结构,以及如何通过循环和JSON编码解码来提取所需的数据,包括属性和文本内容。通过本文,你将学会如何从复杂的XML结构中提取并组织数据,以便在PHP应用程序中使用。
使用 SimpleXML 解析 XML 并提取数据
在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;
?>
登录后复制
代码解释
- 加载 XML 数据: 使用 simplexml_load_string() 函数将 XML 字符串加载到 SimpleXMLElement 对象中。LIBXML_NOCDATA 参数确保 CDATA 部分被视为文本。
- 错误处理: 检查 XML 解析过程中是否发生错误,并输出错误信息。
-
转换为数组:
- 使用 json_encode() 和 json_decode() 函数将 SimpleXMLElement 对象转换为数组。这是一种快速转换复杂结构的方法。
- 移除原始的 ‘Answer’ 键,以便重新组织数据。
- 循环遍历 $objXmlDocument 对象,将每个答案及其属性添加到 $arrOutput 数组中。
输出结果
以上代码将输出以下数组结构:
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',
),
),
)
登录后复制
注意事项
- 错误处理: 在解析 XML 时,务必进行错误处理,以便在 XML 格式不正确时能够及时发现并处理。
- 数据结构: 转换后的数组结构可能需要根据实际需求进行调整。例如,可以使用循环遍历数组,并将数据重新组织成更符合应用程序需求的格式。
- 性能: 对于大型 XML 文件,使用 json_encode() 和 json_decode() 可能会影响性能。可以考虑使用其他更高效的 XML 解析方法,例如 XMLReader。
- 替代方案: 也可以直接使用 SimpleXML 的方法来访问属性和文本内容,例如 $element->attributes()[‘attribute_name’] 和 $element->__toString()。
总结
通过使用 SimpleXML 结合 JSON 编码解码,可以方便地将包含 CDATA 的 XML 数据转换为 PHP 数组。这种方法尤其适用于处理复杂的 XML 结构,并从中提取所需的数据。然而,在实际应用中,需要根据 XML 的具体结构和应用程序的需求,选择最合适的解析方法。
以上就是解析包含CDATA的XML数组数据的详细内容,更多请关注php中文网其它相关文章!