数组转xml

appendChild($p_pXml->createElement($p_strRootNodeName));
	}
	
	foreach ($p_arrData as $mxKey => $mxValue)
	{
		if (is_numeric($mxKey) && is_array($mxValue))
		{
			foreach ($mxValue as $mxSubKey => $mxSubValue)
			{
				if (is_array($mxSubValue))
				{
					$pListNode = $p_pRoot->appendChild($p_pXml->createElement($mxSubKey));
					
					foreach ($mxSubValue as $mxThKey => $mxThValie)
					{
						if (is_string($mxThKey))
						{
							if (XML_FLAG_CDATA_VAL === $mxThKey)
							{
								$pListNode->appendChild($p_pXml->createCDATASection($mxThValie));
							}
							elseif (XML_FLAG_NORM_VAL === $mxThKey)
							{
								$pListNode->appendChild($p_pXml->createTextNode($mxThValie));
							}
							elseif (strpos($mxThKey, XML_FLAG_ATTR) >-1)
							{
								$pListNode->appendChild($p_pXml->createAttribute(substr($mxThKey, strlen(XML_FLAG_ATTR))))->appendChild($p_pXml->createTextNode($mxThValie));
							}
							elseif (XML_FLAG_CONTENT === $mxThKey)
							{
								if (is_array($mxThValie))
								{
									$this->array2Xml($mxThValie, $p_strRootNodeName, $p_pXml, $pListNode);
								}
							}
							else
							{
								$pNode = $pListNode->appendChild($p_pXml->createElement($mxThKey));
								$this->array2Xml($mxThValie, $p_strRootNodeName, $p_pXml, $pNode);
							}
						}
					}
				}
			}
		}
		else
		{
			$mxKey = preg_replace('/[^a-z0-9_]/i', '', $mxKey);
			
			if (XML_FLAG_CDATA_VAL === $mxKey)
			{
				$p_pRoot->appendChild($p_pXml->createCDATASection($mxValue));
			}
			elseif (XML_FLAG_NORM_VAL === $mxKey)
			{
				$p_pRoot->appendChild($p_pXml->createTextNode($mxValue));
			}
			elseif (strpos($mxKey, XML_FLAG_ATTR) > -1)
			{
				$p_pRoot->appendChild($p_pXml->createAttribute(substr($mxKey, strlen(XML_FLAG_ATTR))))->appendChild($p_pXml->createTextNode($mxValue));
			}
			elseif (XML_FLAG_CONTENT === $mxKey)
			{
				if (is_array($mxValue))
				{
					$this->array2Xml($mxValue, $p_strRootNodeName, $p_pXml, $p_pRoot);
				}
			}
			else
			{
				if (is_array($mxValue))
				{
					$pNode = $p_pRoot->appendChild($p_pXml->createElement($mxKey));
					$this->array2Xml($mxValue, $p_strRootNodeName, $p_pXml, $pNode);		
				}
				else
				{
					$p_pRoot->appendChild($p_pXml->createElement($mxKey, htmlentities(trim($mxValue))));
				}
			}
		}
	}
	
	$strXml = $p_pXml->saveXML();
	
	return $strXml;
}
}


//        $arrReturn = array();
//		$arrReturn["r"] = "1";
//		$arrReturn["a"] = "";
//		$arrReturn["d"] = "";
//		
//		for ($i = 0; $i < 5; $i++)
//		{
//			$arrDelFiles = array();
//			$arrDelFiles[XML_FLAG_ATTR . "type"] = $i;
//			$arrDelFiles[XML_FLAG_ATTR . "code"] = "1,2,3,4,5";
//			$arrReturn["d"][]["file"] = $arrDelFiles;
//			$arrAddPath = array();
//			$arrAddPath[XML_FLAG_ATTR . "type"] = $i;
//			$arrAddPath[XML_FLAG_CDATA_VAL]     = "path_111ξ∝ξpath_222ξ∝ξpath_333ξ∝ξpath_444ξ∝ξpath_555";
//			
//			$arrReturn["a"][]["path"] = $arrAddPath;
//		}
		//print_r($arrReturn);
$arr=array(
 			't1'=>array('uid'=>'1001', 'name'=>'jack', 'tel'=>array('uid'=>'1001','name'=>'jack')),
            't2'=>array('uid'=>'1002', 'name'=>'mark', 'tel'=>'1233423'),
 			't3'=>array('uid'=>'1003', 'name'=>'annn', 'tel'=>'1233423'),
			't4'=>array('uid'=>'1004', 'name'=>'cccc', 'tel'=>'1232233'),
 			'attr_id'=>'1001',
 			'attr_name'=>'mark'
);

header('Content-type:text/xml;charset="utf-8"');
$pXml=new util_xml2();
echo $pXml->array2Xml($arr);

?>

你可能感兴趣的:(PHP)