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

使用 php 解析包含 cdata 的 xml 数组

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

使用 SimpleXML 解析 XML 数据

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

$response = '    606    859    616    614/380    812    502    810    740    248    None of the above    ';$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 = '    606    859    616    614/380    812    502    810    740    248    None of the above    ';$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 对象的方法来访问属性和值。推荐使用第二种方法,因为它更加简洁和易于理解。在实际应用中,应根据具体需求选择合适的方法。

以上就是使用 PHP 解析包含 CDATA 的 XML 数组的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1264002.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2025年12月10日 08:06:53
下一篇 2025年12月10日 08:07:08

相关推荐

发表回复

登录后才能评论
关注微信