DOM appenchild at wrong position



I have a problem with appendChild in a XML file. I wrote a little script to edit a XML file. You can change the value of a node or add a new one.



for($i=0;$i<count($value);$i++){
$element = $xpath->query('//parent/child[index="'.$index.'"]//'.$key)->item($i);

if($element){
$element->nodeValue = $value[$i];
} else {
$elementLast = $xpath->query('//parent/child[index="'.$index.'"]//'.$key)->item($i-1);
$elementLast->appendChild($dom_object->createElement($key, $value[$i]));
}}


The new node is created but it looks like this



<child>Blablabla<child>new node</child></child>


So the new node is inserted in the last child node not after it like it should be.


No comments:

Post a Comment