Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating XML node with PHP

Tags:

php

xml

simplexml

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.

like image 246
ptamzz Avatar asked Jan 20 '11 14:01

ptamzz


People also ask

How to update node value in XML using PHP?

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.

Can I use PHP in XML?

If you run the XML file as a PHP script, then yes. The output of the script will be valid XML, but the file that contains the inline PHP will most likely not be valid XML. Save this answer.

How we can change a value of XML document with simple XML?

The way to change the value of an attribute, is to change its text value. This can be done using the setAttribute() method or setting the nodeValue property of the attribute node.

What is XML and node in XML?

Everything in an XML document is a node. For example, the entire document is the document node, and every element is an element node. Root node. The topmost node of a tree. In the case of XML documents, it is always the document node, and not the top-most element.


2 Answers

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');
like image 67
Josh Davis Avatar answered Sep 17 '22 16:09

Josh Davis


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

like image 32
Ward Muylaert Avatar answered Sep 19 '22 16:09

Ward Muylaert