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

再来一个 数组转 XML

php代码
<?php /** * array2xml: a class to convert array in php to xml * it also takes into account attributes names unlike simplexml in php * it returns the xml in form of domdocument class for further manipulation. * it throws exception if the tag name or attribute name has illegal chars. * * author : lalit patel * website: http://www.lalit.org/lab/convert-php-array-to-xml-with-attributes * license: apache license 2.0 * http://www.apache.org/licenses/license-2.0 * version: 0.1 (10 july 2011) * version: 0.2 (16 august 2011) * - replaced htmlentities() with htmlspecialchars() (thanks to liel dulev) * - fixed a edge case where root node has a false/null/0 value. (thanks to liel dulev) * version: 0.3 (22 august 2011) * - fixed tag sanitize regex which didn't allow tagnames with single character. * version: 0.4 (18 september 2011) * - added support for cdata section using @cdata instead of @value. * version: 0.5 (07 december 2011) * - changed logic to check numeric array indices not starting from 0. * version: 0.6 (04 march 2012) * - code now doesn't @cdata to be placed in an empty array * version: 0.7 (24 march 2012) * - reverted to version 0.5 * version: 0.8 (02 may 2012) * - removed htmlspecialchars() before adding to text node or attributes. * * usage: * $xml = array2xml::createxml('root_node_name', $php_array); * echo $xml->savexml(); */ class array2xml { private static $xml = null; private static $encoding = 'utf-8'; /** * initialize the root xml node [optional] * @param $version * @param $encoding * @param $format_output */ public static function init($version = '1.0', $encoding = 'utf-8', $format_output = true) { self::$xml = new domdocument($version, $encoding); self::$xml->formatoutput = $format_output; self::$encoding = $encoding; } /** * convert an array to xml * @param string $node_name - name of the root node to be converted * @param array $arr - aray to be converterd * @return domdocument */ public static function &createxml($node_name, $arr=array()) { $xml = self::getxmlroot(); $xml->appendchild(self::convert($node_name, $arr)); self::$xml = null; // clear the xml node in the class for 2nd time use. return $xml; } /** * convert an array to xml * @param string $node_name - name of the root node to be converted * @param array $arr - aray to be converterd * @return domnode */ private static function &convert($node_name, $arr=array()) { //print_arr($node_name); $xml = self::getxmlroot(); $node = $xml->createelement($node_name); if(is_array($arr)){ // get the attributes first.; if(isset($arr['@attributes'])) { foreach($arr['@attributes'] as $key => $value) { if(!self::isvalidtagname($key)) { throw new exception('[array2xml] illegal character in attribute name. attribute: '.$key.' in node: '.$node_name); } $node->setattribute($key, self::bool2str($value)); } unset($arr['@attributes']); //remove the key from the array once done. } // check if it has a value stored in @value, if yes store the value and return // else check if its directly stored as string if(isset($arr['@value'])) { $node->appendchild($xml->createtextnode(self::bool2str($arr['@value']))); unset($arr['@value']); //remove the key from the array once done. //return from recursion, as a note with value cannot have child nodes. return $node; } else if(isset($arr['@cdata'])) { $node->appendchild($xml->createcdatasection(self::bool2str($arr['@cdata']))); unset($arr['@cdata']); //remove the key from the array once done. //return from recursion, as a note with cdata cannot have child nodes. return $node; } } //create subnodes using recursion if(is_array($arr)){ // recurse to get the node for that key foreach($arr as $key=>$value){ if(!self::isvalidtagname($key)) { throw new exception('[array2xml] illegal character in tag name. tag: '.$key.' in node: '.$node_name); } if(is_array($value) && is_numeric(key($value))) { // more than one node of its kind; // if the new array is numeric index, means it is array of nodes of the same kind // it should follow the parent key name foreach($value as $k=>$v){ $node->appendchild(self::convert($key, $v)); } } else { // only one node of its kind $node->appendchild(self::convert($key, $value)); } unset($arr[$key]); //remove the key from the array once done. } } // after we are done with all the keys in the array (if it is one) // we check if it has any text value, if yes, append it. if(!is_array($arr)) { $node->appendchild($xml->createtextnode(self::bool2str($arr))); } return $node; } /* * get the root xml node, if there isn't one, create it. */ private static function getxmlroot(){ if(empty(self::$xml)) { self::init(); } return self::$xml; } /* * get string representation of boolean value */ private static function bool2str($v){ //convert boolean to text value. $v = $v === true ? 'true' : $v; $v = $v === false ? 'false' : $v; return $v; } /* * check if the tag name or attribute name contains illegal characters * ref: http://www.w3.org/tr/xml/#sec-common-syn */ private static function isvalidtagname($tag){ $pattern = '/^[a-z_]+[a-z0-9\:\-\.\_]*[^:]*$/i'; return preg_match($pattern, $tag, $matches) && $matches[0] == $tag; } } ?>
其它类似信息

推荐信息