1

Assume the following xml document:

<?xml version="1.0"?>
<technicaldata template="123">
    <name>
        <![CDATA[Blub1]]>
    </name>
    <numbers>
        <![CDATA[1
            2
            3
            4
        5]]>
    </numbers>
    <shortinfo>
        <![CDATA[ha ha ha ha ha ha ha ha.]]>
    </shortinfo>
    <detailedinfo>
        <![CDATA[hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi
        hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi ]]>
    </detailedinfo>
</technicaldata>

Now I can load the file via simplexml;

$data='test.xml';
$inhalt = simplexml_load_file($data);

After that I want to replace some specific letters in all parts of the xml file - e.g. all appearances of bar should be replaced by foo... thanks to this great community I know it works with a loop like that:

foreach ($inhalt->children() as $child) {
    $child= str_ireplace('bar', 'foo',$child);
}

The problem is that if I now go e.g. to $inhalt->shortinfo it didn't work tough...

But if I use the loop with an echo like this

foreach ($inhalt->children() as $child) {
    $child= str_ireplace('bar', 'foo',$child);
    echo $child;
}

Then the replacement obviously worked but unfortunately the changes get lost when the forech loop is finished; How can I avoid that? Many thanks!! :)

EDIT: Sorry I talk about php... :)

michi
  • 6,565
  • 4
  • 33
  • 56
J.Doe
  • 281
  • 4
  • 12
  • Use a reference in the loop: `foreach(... as &$child)...` note the `&`. – Tom Regner Nov 06 '15 at 16:16
  • Hey.. Thanks for your help... in doing so I get unfortunately an error; Fatal error: An iterator cannot be used with foreach by reference in C:\xampp\htdocs\getxmldata.php on line 12 – J.Doe Nov 06 '15 at 16:21
  • Ah - xml with simple-xml -- I just saw the loop...; I suggest to use the DOM-API instead if you want to modify the xml-document. http://php.net/manual/en/book.dom.php – Tom Regner Nov 06 '15 at 16:27
  • 1
    So no chance to make it with simplexml? because the whole script is already building on it... – J.Doe Nov 06 '15 at 16:31
  • Possible duplicate of [How is it possible to modify the whole body part of an xml document loaded in php via simplexml?](http://stackoverflow.com/questions/33569274/how-is-it-possible-to-modify-the-whole-body-part-of-an-xml-document-loaded-in-ph) – ThW Nov 06 '15 at 16:46
  • Yes... Somehow yes but I tried making it with dom although I tried it for a long time... :( – J.Doe Nov 06 '15 at 16:54
  • Please don't ask the same question twice, but edit the first question. – michi Nov 07 '15 at 09:53

1 Answers1

1

Much has been said about the hidden powers of SimpleXml, here's how to do it:

$inhalt = simplexml_load_string($x); // assume XML in $x

foreach ($inhalt->children() as $child) {
    $child[0] = str_ireplace('bar', 'foo', $child);
}

echo $inhalt->asXML();

Note the self-reference $child[0] within the foreach loop.
There are a couple of really well explained answers to this technique, see Remove a child with a specific attribute, in SimpleXML for PHP

see the code above working: https://eval.in/464546

Community
  • 1
  • 1
michi
  • 6,565
  • 4
  • 33
  • 56