I have this xml file:
<?xml version="1.0"?>
<products>
<product>
... some nodes
</product>
<product>
... some nodes
</product>
<product>
... some nodes
</product>
</products>
And i wrote this php code:
<?php
....
....
$xml=simplexml_load_file("myfile.xml") or die("Error: Cannot create object");
foreach ($xml->xpath("/products/product") as $prod)
{
$xmlname = $xml->addChild("name",$name);
$xmlid = $xml->addChild("id",$id);
$xmlprice = $xml->addChild("price",$price);
}
$xml->asXML ("result_file.xml");
?>
This works, but not as I want. It creates the file 'result_file.xml' with new nodes: $name,$id and $price. The problem is that all these nodes are inserted at the end of the xml file like this:
<?xml version="1.0"?>
<products>
<product>
... some nodes
</product>
<product>
... some nodes
</product>
<product>
... some nodes
</product>
<name>$name</name>
<id>$id</id>
<price>$price</price>
<name>$name</name>
<id>$id</id>
<price>$price</price>
<name>$name</name>
<id>$id</id>
<price>$price</price>
</products>
But I'd like to insert all these nodes after their product node. For example, i want to obtain:
<?xml version="1.0"?>
<products>
<product>
<name>$name</name>
<id>$id</id>
<price>$price</price>
... some nodes
</product>
<product>
<name>$name</name>
<id>$id</id>
<price>$price</price>
... some nodes
</product>
<product>
<name>$name</name>
<id>$id</id>
<price>$price</price>
... some nodes
</product>
</products>
No comments:
Post a Comment