Joanne Lane schreef: > I am trying to create a class that recursively iterates over an array an > creates XML tree to reflect a multidimensional array. > I am not really a PHP coder, but am trying my hand. I've seen 'real coders' write stuff thats leagues worse. > This is what I have so far. > http://pastie.org/private/w75vyq9ub09p0uawteyieq > > I have tried a few methods, but I keep failing. > Currently, all elements are appended to the root node. line 44 is the issue (kind of), you're always adding to the root node, regardless of 'level' the createNode func needs a little extra something, I played with it for a few minutes and come up with this .. probably not exactly what you need but it might offer a little inspiration (not I also changed the form of the input array a little): <?php class array2xml extends DomDocument { private $xpath; private $root; public function __construct($arr, $root='root') { parent::__construct(); /*** set the encoding ***/ $this->encoding = "ISO-8859-1"; /*** format the output ***/ $this->formatOutput = true; /*** create the root element ***/ $this->root = $this->appendChild($this->createElement( $root )); $this->xpath = new DomXPath($this); } /* * creates the XML representation of the array * * @access public * @param array The array to convert */ public function createNode( $arr, $node = null) { if (is_null($node)) $node = $this->root; foreach($arr as $element => $value) { if (is_numeric($element)) { if (is_array($value)) self::createNode($value, $node); } else { $child = $this->createElement($element, (is_array($value) ? null : $value)); $node->appendChild($child); if (is_array($value)) self::createNode($value, $child); } } } /* * Return the generated XML as a string * * @access public * @return string * */ public function __toString() { return $this->saveXML(); } /* * array2xml::query() - perform an XPath query on the XML representation of the array * @param str $query - query to perform * @return mixed */ public function query($query) { return $this->xpath->evaluate($query); } } $array = array('employees'=> array('employee' => array( array( 'name'=>'bill smith', 'address'=> array('number'=>1, 'street'=>'funny'), 'age'=>25), array( 'name'=>'tom jones', 'address'=> array('number'=>12, 'street'=>'bunny'), 'age'=>88), array( 'name'=>'dudley doright', 'address'=> array('number'=>88, 'street'=>'munny'), 'age'=>23), array( 'name'=>'nellie melba', 'address'=> array('number'=>83, 'street'=>'sunny'), 'age'=>83))) ); try { $xml = new array2xml('root'); $xml->createNode( $array ); echo $xml; } catch (Exception $e) { var_dump($e); } > > How can I fix this so that the XML tree, correctly reflects the array > tree. > > TIA > J > > --------------------------------------------- > http://www.phpwomen.org/ > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php