数组转xml

<?php

define("XML_FLAG_ATTR",      "attr_");
define("XML_FLAG_CDATA_VAL", "cdata_val");
define("XML_FLAG_NORM_VAL",  "norm_val");
define("XML_FLAG_CONTENT",   "sub_cont");
	
final class util_xml2 {

	
 function array2Xml($p_arrData, $p_strRootNodeName = "data", $p_pXml = NUll, $p_pRoot = NULL)
{
	if (NUll === $p_pXml)
	{
		$p_pXml = new DOMDocument("1.0", 'utf-8');
		$p_pRoot = $p_pXml->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);

?>

在Java中,可以使用json-libjar包中的工具类来实现json数组xml。具体步骤如下: 1. 创建一个JSONObject对象,将json字符串换为JSONObject对象。 2. 创建一个Document对象,用于生成XML文档。 3. 遍历JSONObject对象,将每个键值对换为XML元素,并添加到Document对象中。 4. 将Document对象换为XML字符串。 下面是一个简单的示例代码: ``` import net.sf.json.JSONArray; import net.sf.json.JSONObject; import net.sf.json.xml.XMLSerializer; import org.w3c.dom.Document; import org.w3c.dom.Element; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import java.util.Iterator; public class JsonToXml { public static void main(String[] args) throws ParserConfigurationException { String jsonStr = "[{\"name\":\"John\",\"age\":30,\"city\":\"New York\"},{\"name\":\"Jane\",\"age\":25,\"city\":\"San Francisco\"}]"; JSONArray jsonArray = JSONArray.fromObject(jsonStr); Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); Element root = doc.createElement("root"); doc.appendChild(root); Iterator<JSONObject> iterator = jsonArray.iterator(); while (iterator.hasNext()) { JSONObject jsonObject = iterator.next(); Element element = doc.createElement("person"); root.appendChild(element); Iterator<String> keys = jsonObject.keys(); while (keys.hasNext()) { String key = keys.next(); Element subElement = doc.createElement(key); subElement.setTextContent(jsonObject.getString(key)); element.appendChild(subElement); } } XMLSerializer xmlSerializer = new XMLSerializer(); String xmlStr = xmlSerializer.write(doc); System.out.println(xmlStr); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值