您好,欢迎访问一九零五行业门户网

php数组转换成xml文件php类

我们经常会用到缓存数据就是把数组保存成xml文档方便处理,下面我们提供一个数组转换成xml文档的类,有需要的朋友可以参考一下.
php数组转换成xml文件php类代码如下:
$value) { $havetag = true; if (is_numeric($key)) { $key = $keys; $havetag = false; } /** * the first element */ if ($elementlevel == 0) { $startelement = ; $endelement = ; } $text.= $startelement . n; /** * other elements */ if (!$havetag) { $elementlevel++; $text.= . array_xml($value, $key) . n; } else { $elementlevel++; $text.= array_xml($value, $key); } $text.= $endelement . n; } } return $text;}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( $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); } 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();}
永久链接:
转载随意!带上文章地址吧。
其它类似信息

推荐信息