Updating XML node with PHP

how to update node value in xml using php
update xml file using php
change xml node value javascript
php include xml
update xml file using javascript
how to update attribute value in xml using java
how to set node value in xml using java
php xml

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.


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');

Updating XML nodes using SimpleXML adds extra node, using this tutorial you can easily understand that how you can update xml file using php,edit Duration: 2:30 Posted: Dec 21, 2016 You can use a native XML database to facilitate creating, adding, updating and retrieving xml documents and nodes. I've used BerkeleyDBXML (now part of Oracle) in the past with success. There's is a PHP library available as well.


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

XML DOM - Change Node Values, Change language: This function replaces the child oldnode with the passed new node. $newelement = $xml>createTextNode('Some new node!'); Updating XML node with PHP. 0. How to create a basic XML file in PHP. Related. 592. Preferred method to store PHP arrays (json_encode vs serialize) 493.


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.

update xml record using php, PHP has a mechanism to convert XML nodes between SimpleXML and DOM formats. This example shows how one might change a DOM element to SimpleXML. using this tutorial you can easily understand that how you can update xml file using php,edit xml file using php,php change xml node values,how to update node value in xml using php,php simplexml


DOMNode::replaceChild - Manual, simplexml_import_dom — Get a SimpleXMLElement object from a DOM node Also, I wrote a function that can change that array back to XML. <?php function� I want to modify my xml file in PHP based on the following criteria. removing nodes from XML with PHP. 1. How to update Document-A XML nodes with Document-B XML


Basic SimpleXML usage - Manual, I use Laravel 5.8 on php 7.3 and I need to edit some .xml config from Laravel view. I want to click on link, and change enable node property with servername � MySQL Database MySQL Connect MySQL Create DB MySQL Create Table MySQL Insert Data MySQL Get Last ID MySQL Insert Multiple MySQL Prepared MySQL Select Data MySQL Where MySQL Order By MySQL Delete Data MySQL Update Data MySQL Limit Data PHP XML PHP XML Parsers PHP SimpleXML Parser PHP SimpleXML - Get PHP XML Expat PHP XML DOM PHP - AJAX


SimpleXML - Manual, has been added to clipboard! Home| PHP | PHP and XML| SimpleXML: Node and Attribute Updated Oct 15, 2019. By now, you understand� Listed examples may include example.php, which refers to the XML string found in the first example of the basic usage guide. Example #1 Add attributes and children to a SimpleXML element <?php