PHP: How to replace existing XML node with XMLWriter

645

I am editing an XML file and need to populate it with data from a database. DOM works but it is unable to scale to several hundreds of MBs so I am now using XMLReader and XMLWriter which can write the very large XML file. Now, I need to select a node and add children to it but I can't find a method to do it, can someone help me out?

I can find the node I need to add children to by:

if ($xmlReader->nodeType == XMLReader::ELEMENT && $xmlReader->name == 'data')
    {
        echo 'data was found';
        $data = $xmlReader->getAttribute('data');


    }

How do I now add more nodes/children to the found node? Again for clarification, this code will read and find the node, so that is done. What is required is how to modify the found node specifically? Is there a way with XMLWriter for which I have not found a method that will do that after reading through the class documentation?

957

Answer

Solution:

Be default the expanded nodes (missing in your question)

$node = $xmlReader->expand();

are not editable with XMLReader (makes sense by that name). However you can make the specificDOMNode editable if you import it into a newDOMDocument:

$doc  = new DOMDocument();
$node = $doc->importNode($node);

You can then perform any DOM manipulation the DOM offers, e.g. for example adding a text-node:

$textNode = $doc->createTextNode('New Child TextNode added :)');
$node->appendChild($textNode);

If you prefer SimpleXML for manipulation, you can also import the node into SimpleXML after it has been imported into theDOMDocument:

$xml = simplexml_import_dom($node);

An example from above making use of my xmlreader-iterators that just offer me some nicer interface toXMLReader:

$reader  = new XMLReader();
$reader->open($xmlFile);

$elements = new XMLElementIterator($reader, 'data');
foreach ($elements as $element) 
{
    $node = $element->expand();
    $doc  = new DOMDocument();
    $node = $doc->importNode($node, true);
    $node->appendChild($doc->createTextNode('New Child TextNode added :)'));

    echo $doc->saveXML($node), "\n";
}

With the following XML document:

<xml>
    <data/>
    <boo>
        <blur>
            <data/>
            <data/>
        </blur>
    </boo>
    <data/>
</xml>

The small example code above produces the following output:

<data>New Child TextNode added :)</data>
<data>New Child TextNode added :)</data>
<data>New Child TextNode added :)</data>
<data>New Child TextNode added :)</data>

People are also looking for solutions to the problem: php - Cake is NOT able to connect to the database. XAMPP

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.