數(shù)組與XML方法有不少如直接使用遍歷數(shù)組然后生成xml同時(shí)也可以使用DOMDocument來生成xml,具體的方法與步驟如下所示。
PHP將數(shù)組轉(zhuǎn)換成XML:
PHP可以將數(shù)組轉(zhuǎn)換成xml格式,簡單的辦法是遍歷數(shù)組,然后將數(shù)組的key/value轉(zhuǎn)換成xml節(jié)點(diǎn),再直接echo輸出了,如:
- function arrayToXml($arr){
- $xml = "<root>";
- foreach ($arr as $key=>$val){
- if(is_array($val)){
- $xml.="<".$key.">".arrayToXml($val)."</".$key.">";
- }else{
- $xml.="<".$key.">".$val."</".$key.">";
- }
- }
- $xml.="</root>";
- return $xml;
- }
我測試了下,這個(gè)最簡單,速度又快,支持多為數(shù)組,中文也不會亂碼。
另一種方法是利用DOMDocument來生成xml結(jié)構(gòu),代碼如下:
- function arrayToXml($arr,$dom=0,$item=0){
- if (!$dom){
- $dom = new DOMDocument("1.0");
- }
- if(!$item){
- $item = $dom->createElement("root");
- $dom->appendChild($item);
- } //Vevb.com
- foreach ($arr as $key=>$val){
- $itemx = $dom->createElement(is_string($key)?$key:"item");
- $item->appendChild($itemx);
- if (!is_array($val)){
- $text = $dom->createTextNode($val);
- $itemx->appendChild($text);
- }else {
- arrayToXml($val,$dom,$itemx);
- }
- }
- return $dom->saveXML();
- }
它同樣可以將數(shù)組轉(zhuǎn)換成xml,而且支持多維數(shù)組,生成的xml中文也不會亂碼。
PHP將XML轉(zhuǎn)換成數(shù)組:
做接口開發(fā)的時(shí)候經(jīng)常會碰到別人提交給你的是xml格式的數(shù)據(jù),常見的微信接口、支付寶接口等,他們的接口如發(fā)送消息通信都是xml格式的,那么我們先想辦法拿到這個(gè)xml數(shù)據(jù),然后再將其轉(zhuǎn)化成數(shù)組。
假設(shè)我們獲取到一個(gè)這樣的XML,代碼如下:
- <root>
- <user>月光光abcd</user>
- <pvs>13002</pvs>
- <ips>
- <baidu_ip>1200</baidu_ip>
- <google_ip>1829</google_ip>
- </ips>
- <date>2016-06-01</date>
- </root>
通過simplexml_load_string()解析讀取xml數(shù)據(jù),然后先轉(zhuǎn)成json格式,再轉(zhuǎn)換成數(shù)組,代碼如下:
- function xmlToArray($xml){
- //禁止引用外部xml實(shí)體
- libxml_disable_entity_loader(true);
- $xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
- $val = json_decode(json_encode($xmlstring),true);
- return $val;
- }
得到數(shù)組后,我們就可以對數(shù)據(jù)進(jìn)行各種處理了。
下面是網(wǎng)上的,代碼如下:
- class ArrayToXML
- {
- /**
- * The main function for converting to an XML document.
- * Pass in a multi dimensional array and this recrusively
- loops through and builds up an XML document.
- *
- * @param array $data
- * @param string $rootNodeName - what you want the root node to be
- - defaultsto data.
- * @param SimpleXMLElement $xml - should only be used recursively
- * @return string XML
- */
- public static function toXml($data, $rootNodeName = 'data', $xml=null)
- {
- // turn off compatibility mode as simple xml throws a
- wobbly if you don't.
- if (ini_get('zend.ze1_compatibility_mode') == 1)
- {
- ini_set ('zend.ze1_compatibility_mode', 0);
- }
- if ($xml == null)
- {
- $xml = simplexml_load_string("<?xml version='1.0' encoding='utf-8'?><$rootNodeName />");
- }
- // loop through the data passed in.
- foreach($data as $key => $value)
- {
- // no numeric keys in our xml please!
- if (is_numeric($key))
- {
- // make string key...
- $key = "unknownNode_". (string) $key;
- }
- // replace anything not alpha numeric
- $key = preg_replace('/[^a-z]/i', '', $key);
- // if there is another array found recrusively call this function
- if (is_array($value))
- {
- $node = $xml->addChild($key);
- // recrusive call.
- ArrayToXML::toXml($value, $rootNodeName, $node);
- } //Vevb.com
- else
- {
- // add single node.
- $value = htmlentities($value);
- $xml->addChild($key,$value);
- }
- }
- // pass back as string. or simple xml object if you want!
- return $xml->asXML();
- }
- }
下面是我自己編輯的代碼:
- function arrtoxml($arr,$dom=0,$item=0){
- if (!$dom){
- $dom = new DOMDocument("1.0");
- }
- if(!$item){
- $item = $dom->createElement("root");
- $dom->appendChild($item);
- }
- foreach ($arr as $key=>$val){
- $itemx = $dom->createElement(is_string($key)?$key:"item");
- $item->appendChild($itemx);
- if (!is_array($val)){
- $text = $dom->createTextNode($val);
- $itemx->appendChild($text);
- }else {
- arrtoxml($val,$dom,$itemx);
- }
- }
- return $dom->saveXML();
- }
XML轉(zhuǎn)成數(shù)組,代碼如下,如果你使用 curl 獲取的 xml data.
- $xml = simplexml_load_string($data);
- $data['tk'] = json_decode(json_encode($xml),TRUE);
如果是直接獲取 URL 數(shù)據(jù)的話:
- $xml = simplexml_load_file($data);
- $data['tk'] = json_decode(json_encode($xml),TRUE);
先把 simplexml 對象轉(zhuǎn)換成 json,再將 json 轉(zhuǎn)換成數(shù)組。
新聞熱點(diǎn)
疑難解答