If you output the full XML, with echo $xml->asXML()
, you will see that, as you requested, additional child nodes have been added to the first comment node:
<comment>
<user>User4251</user>
<date>02.10.2018</date>
<text>Comment body goes here</text>
<user>User2245</user><date>02.10.2018</date><text>The comment text goes here</text>
</comment>
The reason only the first comment
has been changed is the same reason that your echo
doesn't show the new values: if you reference an element like $xml->comment
or $comment->user
, you get the first child element with that name; it's just short-hand for $xml->comment[0]
or $comment->user[0]
. This is actually really handy for navigating through XML documents, because you don't have to know whether there's one or several elements with a particular name, you can write $xml->comment->user
or $xml->comment[0]->user[0]
or $xml->comment->user[0]
and so on.
Since you called addChild
, the new user
, date
, and text
aren't the first children with that name, so they don't show up in your output.
If what you wanted was to create a new comment, you would need to add that first:
$comment = $xml->addChild('comment');
$comment->addChild('user', 'User2245');
If what you wanted was to change the values of the child elements, you can just write to them instead of adding a new child:
$comment = $xml->comment[0]; // or just $comment = $xml->comment;
$comment->user = 'User2245';
Or you could add something to each of the existing comments (note that here we use $xml->comment
as though it was an array; again, SimpleXML will let us do this whether there's one or several matching elements):
foreach ( $xml->comment as $comment ) {
$comment->addChild('modified', 'true');
}