14

I've an XML file test.xml

<?xml version="1.0"?>
<info>
  <user>
    <name>
      <firstname>FirstName</firstname>
      <lastname>Last Name</lastname>
      <nameCoordinate>
        <xName>125</xName>
        <yName>20</yName>
      </nameCoordinate>
    </name>
  </user>
</info>

I'm trying to update the node xName & yName using PHP on a form submission. So, I've loaded the file using simplexml_load_file(). The PHP form action code is below

<?php 
    $xPostName = $_POST['xName'];
    $yPostName = $_POST['yName'];

    //load xml file to edit
        $xml = simplexml_load_file('test.xml');

    $xml->info->user->name->nameCoordinate->xName = $xPostName;
    $xml->info->user->name->nameCoordinate->yName = $yPostName;
    echo "done";
?>

I want to update the node values but the above code seems to be incorrect. Can anyone help me rectify it??

UPDATE: My question is somewhat similar to this Updating a XML file using PHP but here, I'm loading the XML from an external file and also I'm updating an element, not an attribute. That's where my confusion lies.

3
  • what is not working as expected? you don't have an error explained. Commented Jan 20, 2011 at 14:02
  • I want to update the node values. It's not happening. Commented Jan 20, 2011 at 14:04
  • I've updated my question above. Commented Jan 20, 2011 at 14:07

3 Answers 3

37

You're not accessing the right node. In your example, $xml holds the root node <info/>. Here's a great tip: always name the variable that holds your XML document after its root node, it will prevent such confusion.

Also, as Ward Muylaert pointed out, you need to save the file.

Here's the corrected example:

// load the document
// the root node is <info/> so we load it into $info
$info = simplexml_load_file('test.xml');

// update
$info->user->name->nameCoordinate->xName = $xPostName;
$info->user->name->nameCoordinate->yName = $yPostName;

// save the updated document
$info->asXML('test.xml');
Sign up to request clarification or add additional context in comments.

2 Comments

What will happen if the xName or yName node doesn't already exist?
What if you have several users with several yName and want to edit all of them at once? Any thoughts?
3

You have to write the changes back to the file, use the asXML method of the SimpleXMLElement.

2 Comments

I believe that rewrites the whole tags. I want to update only specific nodes.
It does, but short of writing half the XML interpreting logic in your own code to only write specific nodes, I believe that is as far as you'll get with SimpleXML.
2

try like this.

$xmlDoc = new \DOMDocument;
$xmlDoc->load('Books.xml');
$response = $xmlDoc->getElementsByTagName('Text');

foreach ($response as $node){
        $node->nodeValue = 'test';
    }
$xmlDoc->saveXML();

this might not the best answer but it worked for me.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.