php xml 转换成 数组

全屏阅读
  • 基本信息
  • 作者:
  • 作者已发布:924篇文章
  • 发布时间:2016年12月31日 22:49:58
  • 所属分类:PHP+MySql
  • 阅读次数:2439次阅读
  • 标签:

Php代码

/**
 * 
 * 将xml转为数组
 * @param string $xml xml字符串
 * @param string $version xml版本
 * @param string $charset xml编码
 */
function xmlToArray($xml, $version="1.0", $charset="utf-8"){
	$doc = new DOMDocument ("1.0", $charset);
	$doc->loadXML ($xml);
	$result = domNodeToArray($doc);
	if(isset($result['#document'])){
		$result = $result['#document'];
	}
	return $result;
}
/**
 * 
 * 将domNode转为数组
 * @param DOMNode $oDomNode
 */
function domNodeToArray(DOMNode $oDomNode = null) {
	// return empty array if dom is blank
	if (! $oDomNode->hasChildNodes ()) {
		$mResult = $oDomNode->nodeValue;
	} else {
		$mResult = array ();
		foreach ( $oDomNode->childNodes as $oChildNode ) {
			// how many of these child nodes do we have?
			// this will give us a clue as to what the result structure should be
			$oChildNodeList = $oDomNode->getElementsByTagName ( $oChildNode->nodeName );
			$iChildCount = 0;
			// there are x number of childs in this node that have the same tag name
			// however, we are only interested in the # of siblings with the same tag name
			foreach ( $oChildNodeList as $oNode ) {
				if ($oNode->parentNode->isSameNode ( $oChildNode->parentNode )) {
					$iChildCount ++;
				}
			}
			$mValue = domNodeToArray ( $oChildNode );
			$sKey = ($oChildNode->nodeName {0} == '#') ? 0 : $oChildNode->nodeName;
			$mValue = is_array ( $mValue ) ? $mValue [$oChildNode->nodeName] : $mValue;
			// how many of thse child nodes do we have?
			if ($iChildCount > 1) { // more than 1 child - make numeric array
				$mResult [$sKey] [] = $mValue;
			} else {
				$mResult [$sKey] = $mValue;
			}
		}
		// if the child is <foo>bar</foo>, the result will be array(bar)
		// make the result just 'bar'
		if (count ( $mResult ) == 1 && isset ( $mResult [0] ) && ! is_array ( $mResult [0] )) {
			$mResult = $mResult [0];
		}
	}
	// get our attributes if we have any
	$arAttributes = array ();
	if ($oDomNode->hasAttributes ()) {
		foreach ( $oDomNode->attributes as $sAttrName => $oAttrNode ) {
			// retain namespace prefixes
			$arAttributes ["@{$oAttrNode->nodeName}"] = $oAttrNode->nodeValue;
		}
	}
	// check for namespace attribute - Namespaces will not show up in the attributes list
	if ($oDomNode instanceof DOMElement && $oDomNode->getAttribute ( 'xmlns' )) {
		$arAttributes ["@xmlns"] = $oDomNode->getAttribute ( 'xmlns' );
	}
	if (count ( $arAttributes )) {
		if (! is_array ( $mResult )) {
			$mResult = (trim ( $mResult )) ? array ($mResult ) : array ();
		}
		$mResult = array_merge ( $mResult, $arAttributes );
	}
	$arResult = array ($oDomNode->nodeName => $mResult );
	return $arResult;
}

示例:

 

<?xml version="1.0"?>

<root>

<User>

<UserGUID>123456</UserGUID>

<NickName>aaa@163.com</NickName>

</User>

</root>

 

转换成数组后的形式是:

 

 

Array

(

    [root] => Array

        (

            [User] => Array

                (

                    [UserGUID] => 123456

                    [NickName] => aaa@163.com

                )

 

        )

 

)

顶一下
(0)
100%
订阅 回复
踩一下
(0)
100%
» 郑重声明:本文由mpxq168发布,所有内容仅代表个人观点。版权归恒富网mpxq168共有,欢迎转载, 但未经作者同意必须保留此段声明,并给出文章连接,否则保留追究法律责任的权利! 如果本文侵犯了您的权益,请留言。

目前有 0 条留言 其中:访客:0 条, 博主:0 条

给我留言

您必须 [ 登录 ] 才能发表留言!