How to Insert form value using php to xml file

peteralfredford

New Member
I have sample.xml sheet & I want to enter "URL" & "Priority" values from html form using php scripts.But after running this values are not inserted into the sample.xml file. My xml-schema looks like this:\[code\]<?xml version='1.0' encoding='utf-8'?><urlset xmlns='http://www.google.com/schemas/sitemap/0.84'><url><loc>http://www.menzkart.com/categories/Vest/cid-CU00029312.aspx</loc><priority>1.0</priority></url><url><loc>http://www.menzkart.com/categories/Trackpaints/cid-CU00.aspx</loc><priority>1.0</priority></url></urlset>\[/code\]My Html form page is : \[code\]<html><head><title>xml Information</title></head><body><form action="edit.php" method="post"><table><tr><td>url:</td><td><input type="text" name="url"/></td></tr><tr><td>Priority:</td><td><input type="text" name="priority"/></td></tr><tr><td colspan="2" align="center"><input type="submit" name="submit"/></td></tr></table></form></body></html>\[/code\]My edit.php code that I'm using for insertion into sample.xml is :\[code\]<?php$xmldoc = new DomDocument( '1.0' );$xmldoc->preserveWhiteSpace = false;$xmldoc->formatOutput = true;$url_val = $_POST["url"]$priority_val = $_POST["priority"];echo "url is : ".$url_val;echo "priority is: ".$priority_val;if( $xml = file_get_contents('sample.xml')){$xmldoc->loadXML( $xml, LIBXML_NOBLANKS );// find the urlset tag$root = $xmldoc->getElementsByTagName('urlset')->item(0);// create the <url> tag$url = $xmldoc->createElement('url');// add the url tag before the first element in the <urlset> tag$root->insertBefore( $url, $root->firstChild );// create other elements and add it to the <url> tag.$locElement = $xmldoc->createElement('loc');$url->appendChild($locElement);$locText = $xmldoc->createTextNode($url_val);$locElement->appendChild($locText);$priorityElement = $xmldoc->createElement('priority');$url->appendChild($priorityElement);$priorityText = $xmldoc->createTextNode($priority_val);$priorityElement->appendChild($priorityText);$xmldoc->save('sample.xml');}?>\[/code\]
 
Back
Top