I've got a problem editing an xml file by php. This is my xml, I used DOM to create it:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<node1/>
<node2>
<child id="child1">
<subchild1/>
<subchild2/>
</child>
<child id="child2">
<subchild1/>
<subchild2/>
</child>
<child id="child3">
<subchild1/>
<subchild2/>
</child>
</node2>
</root>
I have to insert text in nodes by id, I'll insert ids for subchilds later... so I used that code:
$dom = new DOMDocument();
$dom->load('documento.xml');
$library = $dom->documentElement;
$xpath = new DOMXPath($dom);
$result = $xpath->query('/root/node2/child[@id="child1"]');
$result->item(0)->nodeValue .= "text";
It worked but it deleted subchild1 and subchild2, so now I've got this, with the space that you can see there:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<node1/>
<node2>
<child id="child1">
text</child>
<child id="child2">
<subchild1/>
<subchild2/>
</child>
<child id="child3">
<subchild1/>
<subchild2/>
</child>
</node2>
</root>
Why subchilds were deleted??