/** * XML encoding * @param mixed $data data * @param string $root root node name * @param string $item Number index sub-section name * @param string $attr root Property * @param string $id Numeric index child node key conversion property name * @param string $encoding Data encoding * @return string*/functionXml_encode ($data,$root= ' Think ',$item= ' Item ',$attr= ",$id= ' id ',$encoding= ' Utf-8 ') { if(Is_array($attr)){ $_attr=Array(); foreach($attr as $key=$value) { $_attr[] = "{$key}=\"{$value}\""; } $attr=implode(‘ ‘,$_attr); } $attr=Trim($attr); $attr=Empty($attr) ? ‘‘ : " {$attr}"; $xml= "<?xml version=\" 1.0\ "encoding=\" {$encoding}\ "?>"; $xml. = "<{$root}{$attr}> "; $xml. = Data_to_xml ($data,$item,$id); $xml. = "</{$root}> "; return $xml;}/** * Data XML encoding * @param mixed $DATA Data * Node name when string $item numeric index * @param string $id numeric index key converted to the property name * @return S Tring*/functionData_to_xml ($data,$item= ' Item ',$id= ' id ') { $xml=$attr= ' '; foreach($data as $key=$val) { if(Is_numeric($key)){ $id&&$attr= " {$id}=\"{$key}\""; $key=$item; } $xml. = "<{$key}{$attr}> "; $xml.= (Is_array($val) ||Is_object($val)) ? Data_to_xml ($val,$item,$id) :$val; $xml. = "</{$key}> "; } return $xml;}
thinkphp XML Encoding functions