PHP中如何处理XML文件?

更新于
2026-09-30 15:05:34
1阅读来源:SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计325个文字,预计阅读时间需要2分钟。

PHP中如何处理XML文件?

一、XML转数组

将XML转换为数组,XML中包含![CDATA[...]]标签。

php

function xml2array($xml, $get_attributes=0, $priority='tag') { if ($get_attributes==0) $get_attributes=1;

$xml_array=array();

if ($priority=='tag') { $children=$xml->getElementsByTagName('*'); } else { $children=$xml->childNodes; }

foreach ($children as $child) { if ($get_attributes==1 && $child->hasAttributes()) { $attributes=$child->attributes(); $child_attr=array(); foreach ($attributes as $attr) { $child_attr[]=array('name'=> $attr->getName(), 'value'=> $attr->getValue()); } } else { $child_attr=array(); }

PHP中如何处理XML文件?

if ($child->nodeType==1) { $node_name=$child->nodeName; if ($get_attributes==0) { $xml_array[$node_name]=(string)$child; } else { $xml_array[$node_name]=array_merge(array('value'=> (string)$child), $child_attr); } } }

return $xml_array;}?>

一、xml转成数组,xml中包含<![CDATA[]]>标签

/** * 将xml转换为数组 * @param string $xml:xml文件或字符串 * @return array */ function xmlToArray($xml){ //考虑到xml文档中可能会包含<![CDATA[]]>标签,第三个参数设置为LIBXML_NOCDATA if (file_exists($xml)) { libxml_disable_entity_loader(false); $xml_string = simplexml_load_file($xml,'SimpleXMLElement', LIBXML_NOCDATA); }else{ libxml_disable_entity_loader(true); $xml_string = simplexml_load_string($xml,'SimpleXMLElement', LIBXML_NOCDATA); } $result = json_decode(json_encode($xml_string),true); return $result; }

二、jsonp转换成数组

/** * 把jsonp转为php数组 * @param string $jsonp jsonp字符串 * @param boolean $assoc 当该参数为true时,将返回array而非object * @return array */ function jsonp_decode($jsonp, $assoc = false) { $jsonp = trim($jsonp); if(isset($jsonp[0]) && $jsonp[0] !== '[' && $jsonp[0] !== '{') { $begin = strpos($jsonp, '('); if(false !== $begin) { $end = strrpos($jsonp, ')'); if(false !== $end) { $jsonp = substr($jsonp, $begin + 1, $end - $begin - 1); } } } return json_decode($jsonp, $assoc); }

相关推荐:《PHP教程》

以上就是PHP-xml & jsonp转数组的方法的详细内容,更多请关注自由互联其它相关文章!

标签:方法一

本文共计325个文字,预计阅读时间需要2分钟。

PHP中如何处理XML文件?

一、XML转数组

将XML转换为数组,XML中包含![CDATA[...]]标签。

php

function xml2array($xml, $get_attributes=0, $priority='tag') { if ($get_attributes==0) $get_attributes=1;

$xml_array=array();

if ($priority=='tag') { $children=$xml->getElementsByTagName('*'); } else { $children=$xml->childNodes; }

foreach ($children as $child) { if ($get_attributes==1 && $child->hasAttributes()) { $attributes=$child->attributes(); $child_attr=array(); foreach ($attributes as $attr) { $child_attr[]=array('name'=> $attr->getName(), 'value'=> $attr->getValue()); } } else { $child_attr=array(); }

PHP中如何处理XML文件?

if ($child->nodeType==1) { $node_name=$child->nodeName; if ($get_attributes==0) { $xml_array[$node_name]=(string)$child; } else { $xml_array[$node_name]=array_merge(array('value'=> (string)$child), $child_attr); } } }

return $xml_array;}?>

一、xml转成数组,xml中包含<![CDATA[]]>标签

/** * 将xml转换为数组 * @param string $xml:xml文件或字符串 * @return array */ function xmlToArray($xml){ //考虑到xml文档中可能会包含<![CDATA[]]>标签,第三个参数设置为LIBXML_NOCDATA if (file_exists($xml)) { libxml_disable_entity_loader(false); $xml_string = simplexml_load_file($xml,'SimpleXMLElement', LIBXML_NOCDATA); }else{ libxml_disable_entity_loader(true); $xml_string = simplexml_load_string($xml,'SimpleXMLElement', LIBXML_NOCDATA); } $result = json_decode(json_encode($xml_string),true); return $result; }

二、jsonp转换成数组

/** * 把jsonp转为php数组 * @param string $jsonp jsonp字符串 * @param boolean $assoc 当该参数为true时,将返回array而非object * @return array */ function jsonp_decode($jsonp, $assoc = false) { $jsonp = trim($jsonp); if(isset($jsonp[0]) && $jsonp[0] !== '[' && $jsonp[0] !== '{') { $begin = strpos($jsonp, '('); if(false !== $begin) { $end = strrpos($jsonp, ')'); if(false !== $end) { $jsonp = substr($jsonp, $begin + 1, $end - $begin - 1); } } } return json_decode($jsonp, $assoc); }

相关推荐:《PHP教程》

以上就是PHP-xml & jsonp转数组的方法的详细内容,更多请关注自由互联其它相关文章!

标签:方法一