data = new DOMDocument('1.0'); $this->dom_tree = $this->data->createElement('result'); $this->data->appendChild($this->dom_tree); $this->recurse_node($array, $this->dom_tree); } private function recurse_node($data, $obj){ $i = 0; foreach($data as $key=>$value){ if(is_array($value)){ $sub_obj[$i] = $this->data->createElement($key); $obj->appendChild($sub_obj[$i]); $this->recurse_node($value, $sub_obj[$i]); } else { $sub_obj[$i] = $this->data->createElement($key, $value); $obj->appendChild($sub_obj[$i]); } $i++; } } public function saveXML(){ return $this->data->saveXML(); } } $test = array( 'cat'=>'animal', 'container'=> array( 'thing'=>'stuff', 'thing2'=>'stuff2', ), 'dog'=>'dumb', ); try { $o_test = new array2xml($test); } catch (Exception $e) { echo $e->getMessage(); } echo $o_test->saveXML(); ?>