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?

1

2 Answers 2

0

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

Sign up to request clarification or add additional context in comments.

2 Comments

Whats the best way of writing the XML back to a file with SimpleXML?
The above question is exactly where I am looking for, but I do not think that a link to a general tutorial can mark this question as answered. How did you solve it using SimpleXML?
0

Link is fine now. I agree with Sarfraz, and this is nice solution to merge XMLs with SImpleXML from comment on PHP manual page, working also with attributes too:

function append_simplexml(&$simplexml_to, &$simplexml_from)
{
    foreach ($simplexml_from->children() as $simplexml_child)
    {
        $simplexml_temp = $simplexml_to->addChild($simplexml_child->getName(), (string) $simplexml_child);
        foreach ($simplexml_child->attributes() as $attr_key => $attr_value)
        {
            $simplexml_temp->addAttribute($attr_key, $attr_value);
        }

        append_simplexml($simplexml_temp, $simplexml_child);
    }
} 

There also is sample of usage.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.