php数组转成xml的代码
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
发布: 2016-07-25 09:04:17
-
-
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("");
- }
-
- // 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);
- }
- 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();
- }
- }
复制代码
2、自己写的php数组转xml的代码
-
-
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();
- }
- ?>
复制代码
|
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
-
2025-02-26 03:58:14
-
2025-02-26 03:38:10
-
2025-02-26 03:17:10
-
2025-02-26 02:49:09
-
2025-02-26 01:08:13
-
2025-02-26 00:46:10
-
2025-02-25 23:42:08
-
2025-02-25 22:50:13
-
2025-02-25 21:54:11
-
2025-02-25 20:45:11
最新问题
-
2025-03-21 13:39:34
-
2025-03-21 13:38:34
-
2025-03-21 13:37:19
-
2025-03-21 13:35:24
-
2025-03-21 13:34:32