note 102361 added to class.simplexmlelement

[email protected]
Newsgroups php.notes
Message-ID <[email protected]>
Simple recursive function to append XML object into SimpleXMLElement node:

<?php
// root: parent element - SimpleXMLElement instance
// append: XML object from simplexml_load_string

function xml_join($root, $append) {
	if ($append) {
		if (strlen(trim((string) $append))==0) {
			$xml = $root->addChild($append->getName());
			foreach($append->children() as $child) {
				xml_join($xml, $child);
			}
		} else {
			$xml = $root->addChild($append->getName(), (string) $append);
		}
		foreach($append->attributes() as $n => $v) {
			$xml->addAttribute($n, $v);
		}
	}
}

$xml_append = simplexml_load_string('<data><items><item id="1">value</item></items></data>');
$xml_root = new SimpleXMLElement('<result></result>');

$cxml = $xml_root->addChild('clone');
xml_join($cxml, $xml_append->items->item[0]);

print $xml_root->asXML();
?>

Result:
<?xml version="1.0"?>
<result>
	<clone>
		<item id="1">
			value
		</item>
	</clone>
</result>
----
Server IP: 147.32.160.17
Probable Submitter: 80.83.68.114
----
Manual Page -- http://www.php.net/manual/en/class.simplexmlelement.php
Edit        -- https://master.php.net/note/edit/102361
Del: integrated  -- https://master.php.net/note/delete/102361/integrated
Del: useless     -- https://master.php.net/note/delete/102361/useless
Del: bad code    -- https://master.php.net/note/delete/102361/bad+code
Del: spam        -- https://master.php.net/note/delete/102361/spam
Del: non-english -- https://master.php.net/note/delete/102361/non-english
Del: in docs     -- https://master.php.net/note/delete/102361/in+docs
Del: other reasons-- https://master.php.net/note/delete/102361
Reject      -- https://master.php.net/note/reject/102361
Search      -- https://master.php.net/manage/user-notes.php
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.