1

This is a sample of my XML document which shows a website sitemap

<?xml version="1.0" encoding="UTF-8"?>
<project name="Esurance 365 Portal">
    <elements>
        <element name="folderone" path="forms/folderone">
            <folder function="model">models</folder>
            <folder function="controller">controllers</folder>
            <folder function="view">views</folder>
        </element>
    </elements>
</project>

I would like to use DOMDocument to add another element node above the one illustrated so that it appears as follows

<?xml version="1.0" encoding="UTF-8"?>
    <project name="Esurance 365 Portal">
        <elements>
           <element name="foldertwo" path="forms/foldertwo">
                <folder function="model">modelstwo</folder>
                <folder function="controller">controllerstwo</folder>
                <folder function="view">viewstwo</folder>
            </element>
            <element name="folderone" path="forms/folderone">
                <folder function="model">models</folder>
                <folder function="controller">controllers</folder>
                <folder function="view">views</folder>
            </element>
        </elements>
    </project>

But from the DOMDocument documentation I cant seem to find even an example that shows me how the new element would be achieved.

The createElement and appendChild methods only show me how to insert the element node but not how to insert the remaining folder child nodes.

1
  • using these functions you should get back a new DOM Node/Element on that you can call appendChild again. Commented Mar 7, 2017 at 12:53

1 Answer 1

1

Select element tag and use cloneNode() to copying it. Using setAttribute() set new value of attribute and using nodeValue set new text in target tag. At the end of code, insert new element before existing element using insertBefore().

$dom = new DOMDocument();
$dom->loadXml($xml);
// select "element" tag and copy it
$element = $dom->getElementsByTagName("element")->item(0);
$clone = $element->cloneNode(true);
// change value of attribute 
$clone->setAttribute("name", "foldertwo");
$clone->setAttribute("path", "forms/foldertwo");
// select "folder" tag in new "element" tag change text of them
$folder = $clone->getElementsByTagName("folder");
$folder->item(0)->nodeValue = "modelstwo";
$folder->item(1)->nodeValue = "controllerstwo";
$folder->item(2)->nodeValue = "viewstwo";
// insert new elements before existing element.
$element->parentNode->insertBefore($clone, $element);
$xml = $dom->saveXml();

See result in demo

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

1 Comment

It does the trick ;-) but I would have also liked to get one which can create the node directly without having to clone an existing one.

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.