xcandiottix Posted July 9, 2010 Share Posted July 9, 2010 I'm a bit frustrated at all the search results i'm getting on google trying to find an answer to how to do this. Everything has loops and arrays and a bunch of coding I don;t really think i need. Here's my problem: I've opened an XML file via simplexml_load_file() and displayed information from one node via xpath. I now want the site visitor to be able to input data into a form and that string will replace the node I am displaying. Here's the layout: XML FILE <user> <settings> <main_table> <banner>http://www.site.com/images/1.gif</banner> </main_table> </settings> </user> PHP FILE: $xml = simplexml_load_file("http://www.site.com/users/".$userid.".xml"); $bannerdata = $xml->xpath("/".$userid."/settings/main_table/Banner"); $banner = $bannerdata[0]; if(isset($_POST['memberbanner'])){ ----here is where I will insert the code to rewrite the XML----- Now, when the user enters a new location for the banner and presses submit the php page needs to access the XML file, go the the node <banner> and change the web address. Any help is appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/207265-edit-xml-with-php/ Share on other sites More sharing options...
BizLab Posted July 9, 2010 Share Posted July 9, 2010 Just an idea here, what about using php's DOM model.. its a little more straightforward in my opinion. <?php $doc = new DOMDocument(); // load the xml file $doc->load( "http://www.site.com/users/".$userid.".xml"); // find the node you want to change and assign it to a variable for better coding $banner = $doc->getElementsByTagName("banner"); // new value $banner->nodeValue = $_POST['memberbanner']; // overwrite the file $doc->saveXML("http://www.site.com/users/".$userid.".xml"); ?> That should just about do it my friend. Let me know if it works for you Here is some additional XML help: http://www.phpfreaks.com/tutorial/handling-xml-data Quote Link to comment https://forums.phpfreaks.com/topic/207265-edit-xml-with-php/#findComment-1083703 Share on other sites More sharing options...
xcandiottix Posted July 9, 2010 Author Share Posted July 9, 2010 Shoot... that looked just too good to be true: Catchable fatal error: Argument 1 passed to DOMDocument::saveXML() must be an instance of DOMNode, string given, called in /html/member/user_page.php on line 51 and defined in /html/includes/member/editpage.php on line 1012 Any idea on that? Line 51 doesn't really have anything to do with this so that's strange. Quote Link to comment https://forums.phpfreaks.com/topic/207265-edit-xml-with-php/#findComment-1083714 Share on other sites More sharing options...
Wolphie Posted July 9, 2010 Share Posted July 9, 2010 This works for me if I'm understanding you correctly: <?php $filename = 'file.xml'; // Check file exists if (file_exists($filename)) { if (isset($_POST['memberbanner'])) { // You may want to add additional sanitizing to this for security $banner = trim($_POST['memberbanner']); // Get the contents and use preg_replace() to replace the text between the <banner> tags $contents = file_get_contents($filename); $contents = preg_replace('/<banner>(.*?)<\/banner>/', '<banner'> . $banner . '</banner>', $contents); // $filename MUST be writeable for this to work file_put_contents($filename, $contents); } else { echo 'Please enter banner text...'; } } else { trigger_error('The file <strong>'. $filename .'</strong> does not exist.', E_USER_ERROR); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/207265-edit-xml-with-php/#findComment-1083719 Share on other sites More sharing options...
xcandiottix Posted July 9, 2010 Author Share Posted July 9, 2010 Regarding the "Catchable fatal error": $doc->saveXML("http://www.site.com/users/".$userid.".xml"); should be $doc->save("http://www.site.com/users/".$userid.".xml"); Then I get a "Cannot open file because of HTTP headers" error I change it to: $doc->save("../users/".$userid.".xml"); No error but the XML file is also unchanged. Wolphie, I'll try your solution next Quote Link to comment https://forums.phpfreaks.com/topic/207265-edit-xml-with-php/#findComment-1083722 Share on other sites More sharing options...
xcandiottix Posted July 9, 2010 Author Share Posted July 9, 2010 Fatal error: The file http://www.site.com/users/Kcandiotti400.xml does not exist. in /html/includes/member/editpage.php on line 1021 Strange. Because I can browse to the XML page in firefox with no problem. Is it a read or write error of some sort? Quote Link to comment https://forums.phpfreaks.com/topic/207265-edit-xml-with-php/#findComment-1083729 Share on other sites More sharing options...
Wolphie Posted July 9, 2010 Share Posted July 9, 2010 Have you tried using a full path instead of a URL? When using the function file_exists() you must use a local path rather than a URL. I.e. /usr/www/htdocs/site.com/xmlfile.xml Quote Link to comment https://forums.phpfreaks.com/topic/207265-edit-xml-with-php/#findComment-1083732 Share on other sites More sharing options...
xcandiottix Posted July 9, 2010 Author Share Posted July 9, 2010 Yeah, just thought of that after checking permissions.... full path works and refreshes the page upon hitting submit but the XML file remains unchanged. This seems like something so easy to accomplish but i'm having a hell of a time Quote Link to comment https://forums.phpfreaks.com/topic/207265-edit-xml-with-php/#findComment-1083733 Share on other sites More sharing options...
Wolphie Posted July 9, 2010 Share Posted July 9, 2010 Have you set the file to write permissions (777)? You must be able to write to the file. I did actually modify the code in my previous post because it wasn't working at first, so you may have copied over the old code. It does definitely work now though, I've tested it. A revised version: <?php $filename = 'path/to/file'; // Check file exists if (file_exists($filename)) { if (isset($_POST['memberbanner'])) { // You may want to add additional sanitizing to this for security $banner = trim($_POST['memberbanner']); // Get the contents and use preg_replace() to replace the text between the <banner> tags $contents = file_get_contents($filename); $contents = preg_replace('/<banner>(.*?)<\/banner>/', '<banner>' . $banner . '</banner>', $contents); file_put_contents($filename, $contents); } } else { trigger_error('The file <strong>'. $filename .'</strong> does not exist.', E_USER_ERROR); } ?> That works perfectly fine for me. Quote Link to comment https://forums.phpfreaks.com/topic/207265-edit-xml-with-php/#findComment-1083734 Share on other sites More sharing options...
xcandiottix Posted July 9, 2010 Author Share Posted July 9, 2010 I got it to work, I recopied your code and also noticed that in my XML file <banner> is actually <Banner> so I made that adjustment. The xml file was changed, I tested "pants" in the input box but the XML file now showed: <main_table> pants </main_table> But with the capital B and your correct code everything looks cherry! Truly, thank you so much for this.. I've been fighting this thing for a week trying to get it. If I can ever help you out in anyway just let me know. PS Thanks for the effort too BizLab .. much appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/207265-edit-xml-with-php/#findComment-1083737 Share on other sites More sharing options...
Wolphie Posted July 9, 2010 Share Posted July 9, 2010 Not a problem please mark the topic as solved! Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/207265-edit-xml-with-php/#findComment-1083738 Share on other sites More sharing options...
BizLab Posted July 9, 2010 Share Posted July 9, 2010 OK man, here you go, tested and working. sorry about the earlier post - needed to eat lunch lol <?php $dom = new DOMDocument(); // header("Content-Type: text/xml"); // uncomment if printing to screen // load the xml file $dom->load("xml-test-worksheet.xml"); // find the node you want to change and assign it to a variable for better coding $banner = $dom->getElementsByTagName("honda"); // new value $banner->item(0)->nodeValue = "Integra"; // echo $dom->saveXML(); // uncomment to print to screen // overwrite the file $dom->save("xml-test-worksheet.xml"); ?> // TEST XML FILE <?xml version="1.0" encoding="utf-8"?> <cars> <honda>Integra</honda> <!-- successful change --> </cars> Quote Link to comment https://forums.phpfreaks.com/topic/207265-edit-xml-with-php/#findComment-1083751 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.