this may sound pretty straight forward, but still I want to post this question in the forum. I have a xml file, which needs to be appended with data after the main element and save the xml file without overwriting the existing xml file but to append the data to already existing data and update the xml file.
For example my xml data looks something similar to this:
<maincontent>
<headercontent>
<product num="2102">
<name>MSG</name>
<category>Wellness</category>
<available content="YES"></available>
</product>
<product num="2101">
<name>YSD</name>
<category>Music</category>
<available content="NO"></available>
</product>
<product num="2100">
<name>RCS</name>
<category>Media</category>
<available content="YES"></available>
</product>
</headercontent>
</maincontent>
I want to add another product with all the info and append the newly added data at the top so that the newly added data should come after the headercontent.
Data to be added:
<product num="2103">
<name>AGB</name>
<category>Movies</category>
<available content="YES"></available>
</product>
The updated xml file should be looking like this as shown below:
<maincontent>
<headercontent>
<product num="2103">
<name>AGB</name>
<category>Movies</category>
<available content="YES"></available>
</product>
<product num="2102">
<name>MSG</name>
<category>Wellness</category>
<available content="YES"></available>
</product>
<product num="2101">
<name>YSD</name>
<category>Music</category>
<available content="NO"></available>
</product>
<product num="2100">
<name>RCS</name>
<category>Media</category>
<available content="YES"></available>
</product>
</headercontent>
</maincontent>
Any useful advice or a piece of example code would be really helpful.
Edit:
sorry guys I haven't posted any php code, my fault. Here is the code which I have been working on:
Thanks
<?php
$xmldoc = new DomDocument();
$xmldoc->formatOutput = true;
$productNum = "2103";
$name = "AGB";
$category = "Movies";
$content = "YES";
if($xml = file_get_contents('main.xml')){
$xmldoc->loadXML($xml);
$root = $xmldoc->firstChild;
$newElement = $xmldoc->createElement('product');
$root->appendChild($newElement);
$numAttribute = $xmldoc->createAttribute("num");
$numAttribute->value = $productNum;
$newElement->appendChild($numAttribute);
$nameElement = $xmldoc->createElement('name');
$root->appendChild($nameElement);
$nameText = $xmldoc->createTextNode($name);
$nameElement->appendChild($nameText);
$categoryElement = $xmldoc->createElement('category');
$root->appendChild($categoryElement);
$categoryText = $xmldoc->createTextNode($category);
$categoryElement->appendChild($categoryText);
$availableElement = $xmldoc->createElement('available');
$root->appendChild($availableElement);
$availableAttribute = $xmldoc->createAttribute("content");
$availableAttribute->value = $content;
$availableElement->appendChild($availableAttribute);
$xmldoc->save('main.xml');
}
?>
My xml file gets updated but the data is added to the firstchild and that too at the bottom, instead I want to add data after and in the beginning as shown above. Here is my output:
<maincontent>
<headercontent>
<product num="2102">
<name>MSG</name>
<category>Wellness</category>
<available content="YES"/>
</product>
<product num="2101">
<name>YSD</name>
<category>Music</category>
<available content="NO"/>
</product>
<product num="2100">
<name>RCS</name>
<category>Media</category>
<available content="YES"/>
</product>
</headercontent>
<product num="2103"/><name>AGB</name><category>Movies</category><available content="YES"/></maincontent>
Any advice?
This will work.
Hope this helps.