Jump to content

Edit XML with PHP


xcandiottix

Recommended Posts

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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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);
}
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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  :wtf:

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.  :D

 

PS Thanks for the effort too BizLab .. much appreciated!

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.