xml - PHP SimpleXML - Multiple children with same name -


i generating xml file following php code. takes form inputs , puts them xml file.

i wanting have multiple children same name. eg:

<person>   <address>     <street>streetname</street>     <streetnumber>streetnumber</streetnumber>   </address>   <address>     <street>streetname</street>     <streetnumber>streetnumber</streetnumber>   </address> </person>   

my code generate xml follows structure;

//add first address - working  $xml->person = ""; $xml->person->address = "";  $xml->person->address->addchild('streetname', $_post['streetname1']); $xml->person->address->addchild('streetnumber', $_post['streetnumber1']);   //attempt add second address, doesn't work  $xml->person->address = "";  $xml->person->address->addchild('streetname', $_post['streetname2']); $xml->person->address->addchild('streetnumber', $_post['streetnumber2']); 

it's easier , less verbose use variables when building xml document. can add multiple <address> elements <person> element this...

$person = $xml->addchild('person');  $address1 = $person->addchild('address'); $address1->addchild('streetname', $_post['streetname1']); $address1->addchild('streetnumber', $_post['streetnumber1']);  $address2 = $person->addchild('address'); $address2->addchild('streetname', $_post['streetname2']); $address2->addchild('streetnumber', $_post['streetnumber2']); 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -