Dymond
Dymond

Reputation: 2277

Update XML node keeps adding value instead of uppdating

I cant seem to get this query right. What I'm trying to achieve is:

Update the value inside my text node.

But the problem is that instead of updating the value it keeps adding the value to the element.

Say I $_POST 'Hello' to the value. It adds value. But later I want to change the value 'Hello' to 'God Bye' instead of modify the node to 'Hello God Bye'.

I have tried I'm using PHP DOM. But can't seem to get it with SimpleXML either.

Any suggestions ?

This is what I have for now:

$xml = new DOMDocument();
$xml->formatOutput = TRUE;
$xml->preserveWhiteSpace = FALSE; 
$xml->load('../stickers.xml');

$xpath = new DOMXPath($xml);

$result = $xpath->query('/stickers/sticker[id="559428"]/text');
$result->item(0)->nodeValue .= 'Hello';

echo $xml->saveXML();
$xml->save('../stickers.xml');

Upvotes: 0

Views: 54

Answers (2)

Volkan
Volkan

Reputation: 2210

Try changing

$result->item(0)->nodeValue .= 'Hello';

into

$result->item(0)->nodeValue = 'Hello';

The dot there is concatenating the strings.

Upvotes: 1

Evert
Evert

Reputation: 99736

You're using .= which is meant for adding onto strings. Replace it with =.

Upvotes: 1

Related Questions