本文档旨在指导开发者如何使用 PHP 解析包含 CDATA 结构的 XML 数据,并将其转换为可用的数组格式。我们将通过 SimpleXML 库加载 XML 数据,并结合 JSON 编码和解码,最终提取出 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 对象可能比较繁琐,我们通常会将其转换为数组。一种常见的方法是先将对象编码为 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 对象的方法来访问属性和值。推荐使用第二种方法,因为它更加简洁和易于理解。在实际应用中,应根据具体需求选择合适的方法。
以上就是使用 PHP 解析包含 CDATA 的 XML 数组的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号