note 78856 added to ref.simplexml

From: Date: Wed, 31 Oct 2007 03:36:25 +0000
Subject: note 78856 added to ref.simplexml
Groups: php.notes 
Request: Send a blank email to php-notes+get-131843@lists.php.net to get a copy of this message
This function could be useful to somebody if you want to insert an XML into another when building an XML from many different files. Note that you must specify a name for the node in which the child files content/node will be inserted : <?php foreach (xxx as $firstCondition){ $xml_parent = simplexml_load_file("$firstCondition.xml"); foreach (yyy as $secondCondition){ $xml_children = simplexml_load_file("secondCondition.xml"); SimpleXMLElementObj_into_xml($xml_parent , $xml_children , 'linkingNode'); //will insert every nodes of the files at the end of the parent_xml } } function SimpleXMLElementObj_into_xml($xml_parent, $xml_children, $linkingNode= "linkingNode" , $child_count = 0 , $xml = false ){ if(!$xml) { $xml = $xml_parent->addChild($linkingNode); }else{ $xml = $xml_parent[$child_count]; } $child_count = 0; foreach($xml_children->children() as $k => $v) { if($xml->$k){ $child_count++; } if($v->children()) { $xml->addChild($k); SimpleXMLElementObj_into_xml($xml->$k, $v, '', $child_count, true); }else{ $xml->addChild($k, $v); } } return $xml; } ?> Thanks to some contributor whom I've taken the structure of this function. ps. sorry for double-posting, it's my first contribution around and made some mistakes... ---- Server IP: 69.147.83.197 Probable Submitter: 129.194.124.2 ---- Manual Page -- http://www.php.net/manual/en/ref.simplexml.php Edit -- https://master.php.net/note/edit/78856 Del: integrated -- https://master.php.net/note/delete/78856/integrated Del: useless -- https://master.php.net/note/delete/78856/useless Del: bad code -- https://master.php.net/note/delete/78856/bad+code Del: spam -- https://master.php.net/note/delete/78856/spam Del: non-english -- https://master.php.net/note/delete/78856/non-english Del: in docs -- https://master.php.net/note/delete/78856/in+docs Del: other reasons-- https://master.php.net/note/delete/78856 Reject -- https://master.php.net/note/reject/78856 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#131843) next »