note 78856 deleted from ref.simplexml by nlopess

From: Date: Wed, 31 Oct 2007 21:23:36 +0000
Subject: note 78856 deleted from ref.simplexml by nlopess
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-131885@lists.php.net to get a copy of this message
Note Submitter: David ---- 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...

« previous php.notes (#131885) next »