这是一个简单的方法,其实是两种方法的其中一种:xml2array 和 array2xml 中的一种啦。
但因为array2xml的时候,没有办法做到更好的把attributes做到更好,因此,就折腾了一些简单的处理方法:
array2xml是hightman的方法的简版。我自己改了一些:
- function array2xml($var, $type = 'root', $tag = '') {
- $ret = '';
- if (!is_int($type)) {
- if ($tag)
- return array2xml(array($tag => $var), 0, $type); else {
- $tag .= $type;
- $type = 0;
- }
- }
- $level = $type;
- $indent = str_repeat("\t", $level);
- if (!is_array($var)) {
- $ret .= $indent . '<' . $tag;
- $var = strval($var);
- if ($var == '') {
- $ret .= ' />';
- } else if (!preg_match('/[^0-9a-zA-Z@\._:\/-]/', $var)) {
- $ret .= '>' . $var . '</' . $tag . '>';
- } else {
- $ret .= "><![CDATA[{$var}]]></{$tag}>";
- }
- // if (strpos($var, "\n") === false){
- // $ret .= '><![CDATA[' . $var . ']]></' . $tag . '>';
- // } else
- $ret .= "\n";
- } else if (!(is_array($var) && count($var) && (array_keys($var) !== range(0, sizeof($var) - 1))) && !emptyempty($var)) {
- foreach ($var as $tmp)
- $ret .= array2xml($tmp, $level, $tag);
- } else {
- $ret .= $indent . '<' . $tag;
- if ($level == 0)
- $ret .= '';
- if (isset($var['@attributes'])) {
- foreach ($var['@attributes'] as $k => $v) {
- if (!is_array($v)) {
- $ret .= sprintf(' %s="%s"', $k, $v);
- }
- }
- unset($var['@attributes']);
- }
- $ret .= ">\n";
- foreach ($var as $key => $val) {
- $ret .= array2xml($val, $level + 1, $key);
- }
- $ret .= "{$indent}</{$tag}>\n";
- }
- return $ret;
在其中强加了attributes。比较恶心的方法啦。。
然后xml2array,其实以前写过,但写的不太好,所以我这次抄的是ibm的网站上的xml2json中的代码:
- define ("DEBUG", false);
- // Maximum Recursion Depth that we can allow.
- define ("MAX_RECURSION_DEPTH_ALLOWED", 25);
- // An empty string
- define ("EMPTY_STR", "");
- // SimpleXMLElement object property name for attributes
- define ("SIMPLE_XML_ELEMENT_OBJECT_PROPERTY_FOR_ATTRIBUTES", "@attributes");
- // SimpleXMLElement object name.
- define ("SIMPLE_XML_ELEMENT_PHP_CLASS", "SimpleXMLElement");
- /**
- * @static
- * @param $simpleXmlElementObject
- * @param int $recursionDepth
- * @return array|null|string
- */
- public static function xml2array($simpleXmlElementObject, $getAttributes = false , &$recursionDepth = 0 ) {
- // Keep an eye on how deeply we are involved in recursion.
- if ($recursionDepth > MAX_RECURSION_DEPTH_ALLOWED) {
- // Fatal error. Exit now.
- return (null);
- }
- if ($recursionDepth == 0) {
- if (get_class($simpleXmlElementObject) != SIMPLE_XML_ELEMENT_PHP_CLASS) {
- // If the external caller doesn't call this function initially
- // with a SimpleXMLElement object, return now.
- return (null);
- } else {
- // Store the original SimpleXmlElementObject sent by the caller.
- // We will need it at the very end when we return from here for good.
- $callerProvidedSimpleXmlElementObject = $simpleXmlElementObject;
- }
- } // End of if ($recursionDepth == 0) {
- if (get_class($simpleXmlElementObject) == SIMPLE_XML_ELEMENT_PHP_CLASS) {
- // Get a copy of the simpleXmlElementObject
- $copyOfsimpleXmlElementObject = $simpleXmlElementObject;
- // Get the object variables in the SimpleXmlElement object for us to iterate.
- $simpleXmlElementObject = get_object_vars($simpleXmlElementObject);
- }
- // It needs to be an array of object variables.
- if (is_array($simpleXmlElementObject)) {
- // Initialize the result array.
- $resultArray = array();
- // Is the input array size 0? Then, we reached the rare CDATA text if any.
- if (count($simpleXmlElementObject) <= 0) {
- // Let us return the lonely CDATA. It could even be
- // an empty element or just filled with whitespaces.
- return (trim(strval($copyOfsimpleXmlElementObject)));
- }
- // Let us walk through the child elements now.
- foreach ($simpleXmlElementObject as $key => $value) {
- // When this block of code is commented, XML attributes will be
- // added to the result array.
- // Uncomment the following block of code if XML attributes are
- // NOT required to be returned as part of the result array.
- /*
- if((is_string($key)) && ($key == SIMPLE_XML_ELEMENT_OBJECT_PROPERTY_FOR_ATTRIBUTES)) {
- continue;
- }
- */
- // Let us recursively process the current element we just visited.
- // Increase the recursion depth by one.
- $recursionDepth++;
- if($key == '@attributes' && $getAttributes == true){
- foreach(self::xml2array($value,$getAttributes, $recursionDepth) as $k=>$v){
- $resultArray[$k]=$v;
- }
- }else{
- $resultArray[$key] = self::xml2array($value,$getAttributes, $recursionDepth);
- }
- // Decrease the recursion depth by one.
- $recursionDepth--;
- } // End of foreach($simpleXmlElementObject as $key=>$value) {
- if ($recursionDepth == 0) {
- // That is it. We are heading to the exit now.
- // Set the XML root element name as the root [top-level] key of
- // the associative array that we are going to return to the caller of this
- // recursive function.
- $tempArray = $resultArray;
- $resultArray = array();
- $resultArray[$callerProvidedSimpleXmlElementObject->getName()] = $tempArray;
- }
- return ($resultArray);
- } else {
- // We are now looking at either the XML attribute text or
- // the text between the XML tags.
- return (trim(strval($simpleXmlElementObject)));
- } // End of else
- }
改过其中的几行代码,否则会报错,改了哪几行我忘了。。官网地址是:http://www.ibm.com/developerworks/xml/library/x-xml2jsonphp/
这里还有一个:http://www.zenme.org/?action=show&id=270,可以参考一下