...

View Full Version : combining xml



timgolding
01-21-2010, 10:07 PM
if i had two xml documents

1:


<products>
<product>
</product>
<product>
</product>
</products>


and 2:


<products>
<product>
</product>
<product>
</product>
</products>


and i wanted to combine them into one xml document



<products>
<product>
</product>
<product>
</product>
<product>
</product>
<product>
</product>
</products>


So that all the product tags were nested between one products tag how would i go about it. I know how to make


<products>
<product>
</product>
<product>
</product>
</products>
<products>
<product>
</product>
<product>
</product>
</products>


but really i want only one parents. Finding it difficult to get my head around the php documentation on DOM.

MattF
01-21-2010, 10:40 PM
<?php

$string1 = '<products>
<product1>
</product>
<product2>
</product>
</products>';

$string2 = '<products>
<product3>
</product>
<product4>
</product>
</products>';

$input = preg_replace('#\r\n|\n#', '', $string1.$string2);
$output = preg_match_all('#<products>(.+?)</products>#', $input, $matches);

print('<products>'."\n".implode("\n", $matches[1])."\n".'</products>'."\n");

?>



That would need a bit of tweaking, but should cover the basic principle.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum