RE: Multi-dimension Array to XML ?
Brought to you by:
bs_php,
nigelswinson
From: Dietrich A. <die...@ga...> - 2001-12-03 00:04:19
|
you could do something like this (again, pseudocode): function recursiveArraytoxml(&$myXML,$array,$contexNode=false){ // if no context node passed, set root node as context if(!$contextNode){ $contextNode = $myXML->getRoot(); } // loop thru array, creating nodes foreach($request as $k => $v){ // recurse if value is an array if(is_array($v)){ $newNode = $myXML->appendChild($node, $k); recursiveArraytoxml($myXML,$newNode,$v); } else { $newNode = $myXML->appendChild($node, $k, $v); $myXML->appendData($newNode, $v); } } } > -----Original Message----- > From: php...@li... > [mailto:php...@li...]On Behalf Of Girish > Nath > Sent: Sunday, December 02, 2001 5:50 PM > To: php...@li... > Cc: Dietrich Ayala > Subject: Re: Multi-dimension Array to XML ? > > > Hi > > Thanks for your code, i've modified it to insert data from my array too, but > i need some advice regarding flexibilty. > > Currently it will deal with an array such as below with no problem. > > $request["mission"]["dest"] = "France"; > $request["mission"]["date"] = "22/07/2001"; > > > But if i add one more level for example : > > > $request["contact"]["email"]["work"] = "j....@mi..."; > $request["contact"]["email"]["home"] = "j....@ao..."; > > It can't handle it, i need to go down atleast one more level, but would also > like to make it future-proof just in-case the project specs get modified > later. > > Thanks for your help. > > > Girish > > > $root = "/request[1]"; > > foreach($request as $k => $v){ > > if(is_array($v)){ > $newNode = $myXML->appendChild($root, $k); > > foreach($v as $a => $b){ > $myXML->appendChild($newNode,$a, $b); > $myXML->appendData($newNode . "/" . $a, $b); > } > > } > else { > $newNode = $myXML->appendChild($root, $k, $v); > $myXML->appendData($newNode, $v); > } > > } > > print $myXML->exportAsXml(); > > > > > ----- Original Message ----- > From: "Dietrich Ayala" <die...@ga...> > To: "Girish Nath" <gi...@bt...> > Sent: Saturday, December 01, 2001 10:14 PM > Subject: RE: Multi-dimension Array to XML ? > > > > here's an example of how to do it, but i can't remember if i got the > phpxml method names/args correct. check the class docs. > > but the basics should be there: > > > > $root = getRoot($phpxml); > > foreach($request as $k => $v){ > > if(is_array($v){ > > $newNode = $phpxml->addChild($root,$k); > > foreach($v as $a => $b){ > > $phpxml->addChild($newNode,$a,$b); > > } > > } else { > > $newNode = $phpxml->addChild($root,$k,$v); > > } > > } > > > > then use the xml export method. > > > > > > > -----Original Message----- > > > From: php...@li... > > > [mailto:php...@li...]On Behalf Of Girish > > > Nath > > > Sent: Saturday, December 01, 2001 2:12 AM > > > To: php...@li... > > > Subject: Multi-dimension Array to XML ? > > > > > > > > > Hi > > > > > > Has anyone got any ideas how to take a multi-dimension array and output > it > > > as XML ? > > > > > > The array for example : > > > > > > $request["agent"] = "James Bond"; > > > $request["codename"] = "007"; > > > $request["expenses"] = "100000000"; > > > > > > $request["mission"]["dest"] = "France"; > > > $request["mission"]["date"] = "22/07/2001"; > > > > > > $request["contact"]["email"]["work"] = "j....@mi..."; > > > $request["contact"]["email"]["home"] = "j....@ao..."; > > > > > > Thank you :) > > > > > > > > > Girish > > > > > > > > > _______________________________________________ > > > Phpxpath-users mailing list > > > Php...@li... > > > https://lists.sourceforge.net/lists/listinfo/phpxpath-users > > > > > > _______________________________________________ > Phpxpath-users mailing list > Php...@li... > https://lists.sourceforge.net/lists/listinfo/phpxpath-users > |