0

I have a DomElement

<title>
   <language id="1" />
   <language id="2" />
</title>

I want to set the content of both languages

$node=$pp->title
foreach($node->language as $lang) {
    $lang->nodeValue="Default";
}

I presumed I had to use ->nodevalue as I do when getting. This gives

 <title>
   <language id="1"><nodeValue>Default</nodeValue></language>
   <language id="2"><nodeValue>Default</nodeValue></language>
</title>

So I tried

$node=$pp->title
foreach($node->language as $lang) {
    $lang="Default";
}

This does not change the value.

I wanted

 <title>
   <language id="1">Default</language>
   <language id="2">Default</language>
</title>

It is probably something very simple I'm overlooking...

Leif Neland
  • 1,416
  • 1
  • 17
  • 40
  • Possible duplicate of [How can I set text value of SimpleXmlElement without using its parent?](http://stackoverflow.com/questions/3153477/how-can-i-set-text-value-of-simplexmlelement-without-using-its-parent) – Leif Neland Jul 02 '16 at 07:56

2 Answers2

1

You need to set to the element itself (not to the variable).

foreach($node->language as $lang) {
    $lang->{0} = "Default";
}
George Pavelka
  • 2,161
  • 11
  • 8
0

The answer is here:

How can I set text value of SimpleXmlElement without using its parent?

$node=$pp->title
foreach($node->language as $lang) {
    $lang[0]="Default";
}
Community
  • 1
  • 1
Leif Neland
  • 1,416
  • 1
  • 17
  • 40