tags:

views:

38

answers:

1

I have these two xml documents:

XML 1:

<outer>
    <foo>
        <foo-child name="fA" special="true">
            content of fA
        </foo-child>
        <foo-child name="fB">
            content of fB
        </foo-child>
    </foo>
    <bar>
        <bar-child name="bA">
            content of bA
        </bar-child>
        <bar-child name="bB" special="true">
            content of bB
        </bar-child>
    </foo>
</outer>

XML 2:

<outer>
    <foo>
        <foo-child name="fA">
            newer and improved content of fA
        </foo-child>
        <foo-child name="fC">
            content of fC
        </foo-child>
    </foo>
    <bar>
        <bar-child name="bA" really-special="true" />
        <bar-child name="bB" special="false">
            Despecialized content of bB
        </bar-child>
    </foo>
</outer>

I want to merge them together such that elements from document two override elements with the same name attribute in document one. If no attribute or content is present in the second document is present, it is inherited from the first. So:

Combined

<outer>
    <foo>
        <foo-child name="fA" special="true">
            newer and improved content of fA
        </foo-child>
        <foo-child name="fB">
            content of fB
        </foo-child>
        <foo-child name="fC">
            content of fC
        </foo-child>
    </foo>
    <bar>
        <bar-child name="bA" really-special="true">
            content of bA
        </bar-child>
        <bar-child name="bB">
            Despecialized content of bB
        </bar-child>
    </foo>
</outer>

My intent is to do this server-side with PHP.

However, i'm unsure whether this is a job for DomDocument or SimpleXML. I haven't really used either, and keep thinking "I wish I had jQuery".

Is there a simple solution to this problem?

A: 

Going with SimpleXML is the right path there and probably easier one. Here is a good tutorial on how to use it:

Introduction To SimpleXML With PHP

Sarfraz
Whats the best way of writing the XML back to a file with SimpleXML?
Eric