国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > PHP > 正文

php數(shù)組轉(zhuǎn)換成xml文件php類

2024-05-04 21:58:38
字體:
供稿:網(wǎng)友

我們經(jīng)常會(huì)用到緩存數(shù)據(jù)就是把數(shù)組保存成xml文檔方便處理,下面我們提供一個(gè)數(shù)組轉(zhuǎn)換成xml文檔的類,有需要的朋友可以參考一下.

php數(shù)組轉(zhuǎn)換成xml文件php類代碼如下:

  1. <?php 
  2. $elementLevel = 0 ;  
  3. function array_Xml($array$keys = '')  
  4. {  
  5. global $elementLevel;  
  6. if(!is_array($array))  
  7. {  
  8. if($keys == ''){  
  9. return $array;  
  10. }else{  
  11. return "n<$keys>" . $array . "</$keys>";  
  12. //開源代碼Vevb.com 
  13. }else{  
  14. foreach ($array as $key => $value)  
  15. {  
  16. $haveTag = true;  
  17. if (is_numeric($key))  
  18. {  
  19. $key = $keys;  
  20. $haveTag = false;  
  21. }  
  22. /**  
  23. * The first element  
  24. */  
  25. if($elementLevel == 0 )  
  26. {  
  27. $startElement = "<$key>";  
  28. $endElement = "</$key>";  
  29. }  
  30. $text .= $startElement."n";  
  31. /**  
  32. * Other elements  
  33. */  
  34. if(!$haveTag)  
  35. {  
  36. $elementLevel++;  
  37. $text .= "<$key>" . array_Xml($value$key). "</$key>n";  
  38. }else{  
  39. $elementLevel++;  
  40. $text .= array_Xml($value$key);  
  41. }  
  42. $text .= $endElement."n";  
  43. }  
  44. }  
  45. return $text;  
  46. }  
  47.  
  48. class ArrayToXML  
  49. {  
  50. /**  
  51. * The main function for converting to an XML document.  
  52. * Pass in a multi dimensional array and this recrusively loops教程 through and builds up an XML document.  
  53.  
  54. * @param array $data  
  55. * @param string $rootNodeName - what you want the root node to be - defaultsto data.  
  56. * @param SimpleXMLElement $xml - should only be used recursively  
  57. * @return string XML  
  58. */  
  59. public static function toXml($data$rootNodeName = 'data'$xml=null)  
  60. {  
  61. // turn off compatibility mode as simple xml throws a wobbly if you don't.  
  62. if (ini_get('zend.ze1_compatibility_mode') == 1)  
  63. {  
  64. ini_set ('zend.ze1_compatibility_mode', 0);  
  65. }  
  66. if ($xml == null)  
  67. {  
  68. $xml = simplexml_load_string("<?xml version='1.0' encoding='utf-8'?><$rootNodeName />");  
  69. }  
  70. // loop through the data passed in.  
  71. foreach($data as $key => $value)  
  72. {  
  73. // no numeric keys in our xml please!  
  74. if (is_numeric($key))  
  75. {  
  76. // make string key...  
  77. $key = "unknownNode_". (string) $key;  
  78. }  
  79. // replace anything not alpha numeric  
  80. $key = preg_replace('/[^a-z]/i'''$key);  
  81. // if there is another array found recrusively call this function  
  82. if (is_array($value))  
  83. {  
  84. $node = $xml->addChild($key);  
  85. // recrusive call.  
  86. ArrayToXML::toXml($value$rootNodeName$node);  
  87. }  
  88. else  
  89. {  
  90. // add single node.  
  91. $value = htmlentities($value);  
  92. $xml->addChild($key,$value);  
  93. }  
  94. }  
  95. // pass back as string. or simple xml object if you want!  
  96. return $xml->asXML();  
  97. }  
  98.  
  99.  
  100. function arrtoxml($arr,$dom=0,$item=0){  
  101. if (!$dom){  
  102. $dom = new DOMDocument("1.0");  
  103. }  
  104. if(!$item){  
  105. $item = $dom->createElement("root");  
  106. $dom->appendChild($item);  
  107. }  
  108. foreach ($arr as $key=>$val){  
  109. $itemx = $dom->createElement(is_string($key)?$key:"item");  
  110. $item->appendChild($itemx);  
  111. if (!is_array($val)){  
  112. $text = $dom->createTextNode($val);  
  113. $itemx->appendChild($text);  
  114. }else {  
  115. arrtoxml($val,$dom,$itemx);  
  116. }  
  117. }  
  118. return $dom->saveXML();  
  119. ?> 

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 秦皇岛市| 大田县| 丹阳市| 锡林郭勒盟| 黑水县| 津市市| 义乌市| 冷水江市| 延边| 奉贤区| 枣强县| 台东县| 安溪县| 英吉沙县| 德格县| 拜泉县| 楚雄市| 广水市| 吉木萨尔县| 郁南县| 隆子县| 辛集市| 汝州市| 丰台区| 苏州市| 秭归县| 科技| 英超| 肥乡县| 久治县| 沅陵县| 同江市| 昆山市| 商洛市| 华宁县| 正蓝旗| 浮梁县| 屏南县| 文化| 阿勒泰市| 麻阳|