note 78855 added to ref.simplexml
| From: | David at osu1 dot php dot net | Date: | Wed, 31 Oct 2007 03:32:10 +0000 |
| Subject: | note 78855 added to ref.simplexml | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-131842@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 :
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.
----
Server IP: 194.153.94.10
Probable Submitter: 129.194.124.2
----
Manual Page -- http://www.php.net/manual/en/ref.simplexml.php
Edit -- https://master.php.net/note/edit/78855
Del: integrated -- https://master.php.net/note/delete/78855/integrated
Del: useless -- https://master.php.net/note/delete/78855/useless
Del: bad code -- https://master.php.net/note/delete/78855/bad+code
Del: spam -- https://master.php.net/note/delete/78855/spam
Del: non-english -- https://master.php.net/note/delete/78855/non-english
Del: in docs -- https://master.php.net/note/delete/78855/in+docs
Del: other reasons-- https://master.php.net/note/delete/78855
Reject -- https://master.php.net/note/reject/78855
Search -- https://master.php.net/manage/user-notes.php