note 106876 modified in ref.simplexml by danbrown

From: Date: Tue, 03 Jan 2012 22:11:44 +0000
Subject: note 106876 modified in ref.simplexml by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-185329@lists.php.net to get a copy of this message
Merge two xml only with SimpleXML : Function corrected if note containe only value. <?php function mergeXML(&$base, $add) { if ( $add->count() != 0 ) $new = $base->addChild($add->getName()); else $new = $base->addChild($add->getName(), $add); foreach ($add->attributes() as $a => $b) { $new->addAttribute($a, $b); } if ( $add->count() != 0 ) { foreach ($add->children() as $child) { mergeXML($new, $child); } } } ?> Use : <?php mergeXML($xmlElement, simplexml_load_string($toMerge)) ?> --was-- Merge two xml only with SimpleXML : Function corrected if note containe only value. function mergeXML(&$base, $add) { if ( $add->count() != 0 ) $new = $base->addChild($add->getName()); else $new = $base->addChild($add->getName(), $add); foreach ($add->attributes() as $a => $b) { $new->addAttribute($a, $b); } if ( $add->count() != 0 ) { foreach ($add->children() as $child) { mergeXML($new, $child); } } } Use : <?php mergeXML($xmlElement, simplexml_load_string($toMerge)) ?> http://php.net/manual/en/ref.simplexml.php

« previous php.notes (#185329) next »