Scal pliki XML w PHP

Mam 2 pliki1.xml i2.xml obie mają podobną strukturę i chciałbym je mieć. Próbowałem wielu rozwiązań, ale miałem tylko błędy - mówiąc szczerze, nie mam pojęcia, jak działają te skrypty.

1.xml:

<code><res>
    <items total="180">
        <item>
            <id>1</id>
            <title>Title 1</title>
            <author>Author 1</author>
        </item>
        ...
    </items>
</res> 
</code>

2.xml:

<code><res>
    <items total="123">
        <item>
            <id>190</id>
            <title>Title 190</title>
            <author>Author 190</author>
        </item>
        ...
    </items>
</res> 
</code>

Chciałbym utworzyć nowy plikmerged.xml o następującej strukturze

<code><res>
    <items total="303">
        <item>
            <id>1</id>
            <title>Title 1</title>
            <author>Author 1</author>
        </item>
        ...  //items from 1.xml
        <item>
            <id>190</id>
            <title>Title 190</title>
            <author>Author 190</author>
        </item>
        ... //items from 2.xml
    </items>
</res> 
</code>

Jak mam to zrobić? Czy możesz wyjaśnić mi, jak to zrobić? Jak mogę to zrobić z większą ilością plików? Dzięki

Edytować

Co próbowałem?

<code><?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);
        }
    }
}
$xml = mergeXML(simplexml_load_file('1.xml'), simplexml_load_file('2.xml'));
echo $xml->asXML(merged.xml);
?>
</code>

EDIT2

Podążając za Torious Rade, przejrzałem podręcznik DOMDocument i znalazłem przykład:

<code>function joinXML($parent, $child, $tag = null)
{
    $DOMChild = new DOMDocument;
    $DOMChild->load($child);
    $node = $DOMChild->documentElement;

    $DOMParent = new DOMDocument;
    $DOMParent->formatOutput = true;
    $DOMParent->load($parent);

    $node = $DOMParent->importNode($node, true);

    if ($tag !== null) {
        $tag = $DOMParent->getElementsByTagName($tag)->item(0);
        $tag->appendChild($node);
    } else {
        $DOMParent->documentElement->appendChild($node);
    }

    return $DOMParent->save('merged.xml');
}

joinXML('1.xml', '2.xml')
</code>

Ale tworzy niewłaściwy plik xml:

<code><res>
    <items total="180">
        <item>
            <id>1</id>
            <title>Title 1</title>
            <author>Author 1</author>
        </item>
        ...
    </items>
    <res>
        <items total="123">
            <item>
                <id>190</id>
                <title>Title 190</title>
                <author>Author 190</author>
            </item>
            ...
        </items>
    </res> 
</res>  
</code>

I nie mogę poprawnie użyć tego pliku. Potrzebuję poprawnej struktury i tutaj mam rodzaj wklejania jednego pliku do drugiego. Chciałbym „wkleić” tylko tagi nie wszystkich elementów. Co powinienem zmienić?

EDIT3

Oto odpowiedź - oparta na odpowiedzi Torious - po prostu dostosowana do moich potrzeb - zaznacz // edytowane

<code>$doc1 = new DOMDocument();
$doc1->load('1.xml');

$doc2 = new DOMDocument();
$doc2->load('2.xml');

// get 'res' element of document 1
$res1 = $doc1->getElementsByTagName('items')->item(0); //edited res - items

// iterate over 'item' elements of document 2
$items2 = $doc2->getElementsByTagName('item');
for ($i = 0; $i < $items2->length; $i ++) {
    $item2 = $items2->item($i);

    // import/copy item from document 2 to document 1
    $item1 = $doc1->importNode($item2, true);

    // append imported item to document 1 'res' element
    $res1->appendChild($item1);

}
$doc1->save('merged.xml'); //edited -added saving into xml file
</code>

questionAnswers(3)

yourAnswerToTheQuestion