Thread: Re: Multi-dimension Array to XML ?
Brought to you by:
bs_php,
nigelswinson
From: Girish N. <gi...@bt...> - 2001-12-02 22:54:16
|
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 > > |
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 > |
From: Girish N. <gi...@bt...> - 2001-12-03 01:29:39
|
Hi I'm trying to modify this code but running into some problems. I've noticed your using a getRoot() function as well as passing what seem like multiple nodenames to appendChild() This doesn't seem to be in v2.1 on sourceforge.net, are you using some kind of dev version? Thanks Girish ----- Original Message ----- From: "Dietrich Ayala" <die...@ga...> To: "Girish Nath" <gi...@bt...>; <php...@li...> Sent: Monday, December 03, 2001 12:10 AM Subject: RE: Multi-dimension Array to XML ? > 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 > > > > _______________________________________________ > Phpxpath-users mailing list > Php...@li... > https://lists.sourceforge.net/lists/listinfo/phpxpath-users |
From: Dietrich A. <die...@ga...> - 2001-12-03 01:40:30
|
i'm just writing out pseudocode off the top of my head, cause i haven't used the newer versions of the class :) there is (was?) a method that returned the root node (if there isn't, there should be). if you already know your root node, just cut that part out, and pass your root node in. it was just a convenience thang anyway. and i wasn't passing 2 nodes to appendChild, i was assuming you could call it like so: appendChild($node,$newNodeName,$newNodeValue). looks like you must call appendData() to set element content, so just cut off the ",$v" from that part. > -----Original Message----- > From: Girish Nath [mailto:gi...@bt...] > Sent: Sunday, December 02, 2001 8:27 PM > To: Dietrich Ayala; php...@li... > Subject: Re: Multi-dimension Array to XML ? > > > Hi > > I'm trying to modify this code but running into some problems. > > I've noticed your using a getRoot() function as well as passing what seem > like multiple nodenames to appendChild() > > This doesn't seem to be in v2.1 on sourceforge.net, are you using some kind > of dev version? > > Thanks > > > Girish > > > > > ----- Original Message ----- > From: "Dietrich Ayala" <die...@ga...> > To: "Girish Nath" <gi...@bt...>; > <php...@li...> > Sent: Monday, December 03, 2001 12:10 AM > Subject: RE: Multi-dimension Array to XML ? > > > > 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 > > > > > > > _______________________________________________ > > Phpxpath-users mailing list > > Php...@li... > > https://lists.sourceforge.net/lists/listinfo/phpxpath-users > > > |