Update / add data to XML file using php

it may sound pretty straightforward, but still I want to post this question on the forum. I have an xml file that needs to be added with data after the main element and save the xml file without overwriting the existing xml file, but to add data to existing data and update the XML file.

For example, my xml data looks something like 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 information and add the recently added data at the top so that the new added data appears after the contents of the header.

Data to add:

  <product num="2103"> <name>AGB</name> <category>Movies</category> <available content="YES"></available> </product> 

The updated xml file should look like the one 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 helpful tip or piece of sample code would be really helpful.

Edit:

Sorry guys, I haven't written a single php code, my fault. Here is the code I was 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 is updated, but the data is added to the firstchild, and this is also at the bottom, instead I want to add data after and at the beginning, as shown above. Here is my conclusion:

 <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 tips?

+4
source share
1 answer

This will work.

 <?php $xmldoc = new DomDocument( '1.0' ); $xmldoc->preserveWhiteSpace = false; $xmldoc->formatOutput = true; $productNum = "2103"; $name = "AGB"; $category = "Movies"; $content = "YES"; if( $xml = file_get_contents( 'main.xml') ) { $xmldoc->loadXML( $xml, LIBXML_NOBLANKS ); // find the headercontent tag $root = $xmldoc->getElementsByTagName('headercontent')->item(0); // create the <product> tag $product = $xmldoc->createElement('product'); $numAttribute = $xmldoc->createAttribute("num"); $numAttribute->value = $productNum; $product->appendChild($numAttribute); // add the product tag before the first element in the <headercontent> tag $root->insertBefore( $product, $root->firstChild ); // create other elements and add it to the <product> tag. $nameElement = $xmldoc->createElement('name'); $product->appendChild($nameElement); $nameText = $xmldoc->createTextNode($name); $nameElement->appendChild($nameText); $categoryElement = $xmldoc->createElement('category'); $product->appendChild($categoryElement); $categoryText = $xmldoc->createTextNode($category); $categoryElement->appendChild($categoryText); $availableElement = $xmldoc->createElement('available'); $product->appendChild($availableElement); $availableAttribute = $xmldoc->createAttribute("content"); $availableAttribute->value = $content; $availableElement->appendChild($availableAttribute); $xmldoc->save('main.xml'); } ?> 

Hope this helps.

+7
source

All Articles