使用 PHP 解析包含 CDATA 的 XML 数组

使用 PHP 解析包含 CDATA 的 XML 数组

本文档旨在指导开发者如何使用 php 解析包含 CDATA 结构的 xml 数据,并将其转换为可用的数组格式。我们将通过 simpleXML 库加载 XML 数据,并结合 json 编码和解码,最终提取出 XML 结构中的属性和值。本文提供详细的代码示例和解释,帮助读者理解和应用该方法。

使用 SimpleXML 解析 XML 数据

PHP 的 SimpleXML 扩展提供了一种简单的方式来处理 XML 文档。 我们可以使用 simplexml_load_string() 函数将 XML 字符串加载到 SimpleXMLElement 对象中。

$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; }

这段代码首先定义了一个 XML 字符串,然后使用 simplexml_load_string() 函数将其解析为一个 SimpleXMLElement 对象。如果解析过程中发生错误,代码会输出错误信息并退出。LIBXML_NOCDATA 选项指示 libxml 处理 CDATA 节点。

将 SimpleXMLElement 对象转换为数组

由于直接处理 SimpleXMLElement 对象可能比较繁琐,我们通常会将其转换为数组。一种常见的方法是先将对象编码为 JSON 字符串,然后再解码为 PHP 数组。

立即学习PHP免费学习笔记(深入)”;

$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;

这段代码首先使用 json_encode() 函数将 SimpleXMLElement 对象编码为 JSON 字符串,然后使用 json_decode() 函数将其解码为 PHP 数组。 unset($arrOutput[‘Answer’]); 是为了清除掉初始的Answer,因为后面要重新组装。循环遍历 $objXmlDocument 对象,并为每个子元素(例如,Answer)创建一个新的数组。

输出结果分析

上述代码的输出结果如下:

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 结构中的属性(例如,type 和 text)被存储在 @attributes 键下,而元素的值则作为数组的元素。

更优雅的处理方式

虽然上面的方法可以实现 XML 到数组的转换,但可能不够优雅。更推荐的方法是直接使用 SimpleXMLElement 对象的方法来访问属性和值。

$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; }  $question = [     'type' => (string) $objXmlDocument['type'],     'text' => (string) $objXmlDocument['text'],     'answers' => [], ];  foreach ($objXmlDocument->Answer as $answer) {     $question['answers'][] = [         'correct' => (string) $answer['correct'],         'value' => (string) $answer,     ]; }  echo var_export($question, true) . PHP_EOL;

这段代码直接访问 SimpleXMLElement 对象的属性和值,并将它们存储在一个新的数组中。这种方法更加简洁和易于理解。

总结

本文介绍了两种使用 PHP 解析包含 CDATA 结构的 XML 数据的方法。第一种方法使用 JSON 编码和解码将 SimpleXMLElement 对象转换为数组。第二种方法直接使用 SimpleXMLElement 对象的方法来访问属性和值。推荐使用第二种方法,因为它更加简洁和易于理解。在实际应用中,应根据具体需求选择合适的方法。

© 版权声明
THE END
喜欢就支持一下吧
点赞10 分享