You are here: Re: Copying XML nodes with DOM « All PHP « IT news, forums, messages
Re: Copying XML nodes with DOM

Posted by Janwillem Borleffs on 06/18/06 14:38

Adriαn Ribao wrote:
> So, I can't do it with ImportNode and AppendChild. I have no idea how
> to do it.
> Can someone help me please?
>

The following isn't pretty, but might give you some ideas, as there doesn't
seem to be a straightforward way of doing what you want:

<?php

$xml =<<<EOX
<root>
<one />
<two>
Hello
<three>World</three>
</two>
</root>
EOX;

$xml2 =<<<EOX
<whatever>
<node />
</whatever>
EOX;

function appendChildNodes(DomDocument $targetdoc,
DOMNode $targetnode,
DOMNodeList $nodelist) {
foreach ($nodelist as $item) {
$node = $targetdoc->importNode($item, true);
$targetnode->appendChild($node);
}
}

$doc = DomDocument::loadXML($xml);
$doc2 = DomDocument::loadXML($xml2);
$source = $doc->documentElement
->getElementsByTagName('two')->item(0);
$target = $doc2->documentElement
->getElementsByTagName('node')->item(0);

appendChildNodes($doc2, $target, $source->childNodes);
print $doc2->saveXML();

?>


HTH;
JW

 

Navigation:

[Reply to this message]


УдалСнная Ρ€Π°Π±ΠΎΡ‚Π° для программистов  •  Как Π·Π°Ρ€Π°Π±ΠΎΡ‚Π°Ρ‚ΡŒ Π½Π° Google AdSense  •  England, UK  •  ΡΡ‚Π°Ρ‚ΡŒΠΈ Π½Π° английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Π‘Π°ΠΉΡ‚ ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ Π² Π‘Ρ‚ΡƒΠ΄ΠΈΠΈ Π’Π°Π»Π΅Π½Ρ‚ΠΈΠ½Π° ΠŸΠ΅Ρ‚Ρ€ΡƒΡ‡Π΅ΠΊΠ°
ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ΠΈΠ΅ ΠΈ ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΊΠ° Π²Π΅Π±-сайтов, Ρ€Π°Π·Ρ€Π°Π±ΠΎΡ‚ΠΊΠ° ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΠ½ΠΎΠ³ΠΎ обСспСчСния, поисковая оптимизация