参考
http://stackoverflow.com/questions/1397036/how-to-convert-array-to-simplexml
<?php class FileTools { /** * 内部函数 * * @param array $arr * @param SimpleXMLElement $xml * @return SimpleXMLElement */ static function _array_to_xml(array $arr, SimpleXMLElement $xml) { foreach ( $arr as $k => $v ) { $attrArr = array (); $kArray = explode ( ' ', $k ); $tag = array_shift ( $kArray ); if (count ( $kArray ) > 0) { foreach ( $kArray as $attrValue ) { $attrArr [] = explode ( '=', $attrValue ); } } if (is_array ( $v )) { if (is_numeric ( $k )) { self::_array_to_xml ( $v, $xml ); } else { $child = $xml->addChild ( $tag ); if (isset ( $attrArr )) { foreach ( $attrArr as $attrArrV ) { $child->addAttribute ( $attrArrV [0], $attrArrV [1] ); } } self::_array_to_xml ( $v, $child ); } } else { $child = $xml->addChild ( $tag, $v ); if (isset ( $attrArr )) { foreach ( $attrArr as $attrArrV ) { $child->addAttribute ( $attrArrV [0], $attrArrV [1] ); } } } } return $xml; } /** * * @param array $array * 需要转化的数组 * @param string $root * 根标签 * @param boolean $formatoutput * 是否需要格式化输出 * @param boolean $preserveWhiteSpace * 是否过滤标签之间的空白 * @throws Exception * @return mixed Ambigous mixed> */ static function array_to_xml($array = array(), $root, $formatoutput = false, $preserveWhiteSpace = false) { if (empty ( $root )) { throw new Exception ( '根标签不能为空' ); } if (! preg_match ( '/^<w+/?>$/', $root )) { throw new Exception ( '标签格式异常' ); } $xml = self::_array_to_xml ( $array, new SimpleXMLElement ( $root ) )->asXML (); if ($formatoutput || $preserveWhiteSpace) { if (! class_exists ( "DOMDocument" )) return $xml; $dom = new DOMDocument (); $dom->preserveWhiteSpace = $preserveWhiteSpace; $dom->loadXML ( $xml ); $dom->formatOutput = $formatoutput; $xml = $dom->saveXml (); } return $xml; } }