I’ve been working on a script to open an XML file, insert a node, then save the file again.
[code=php]
$url = ‘../v3flashslideshow/slideshow_data0.xml’;
$xml = new DomDocument();
$xml->formatOutput = true;
$xml->preserveWhitespace = false;
$xml->load($url);
$xpath = new DOMXPath($xml);
$first = $xpath->query(‘//galleries/gallery/img’)->item(0);
// create node
$new = $xml->createElement(“img”);
$xml->appendChild($new);
$image = $xml->createAttribute(“src”);
$new->appendChild($image);
$new->setAttribute(“src”, $_GET[‘image’]);
$first->parentNode->insertBefore($new, $first);
echo $xml->save($url);
When it writes the file, the new node is added twice.
When I change the last line to:
[code=php]
echo $xml->saveXML();
just to display it on the screen, it works as it is supposed to and the new node only appears once.
Any ideas anyone?
Thanks in advance.