Updating XML node with PHP
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');
How to update xml file using PHP?
Your program shows the form firstly, then saves data secondly.
So when you click button submit, the program shows the form with previous data first and then,
saves 'new' data.
Therefore following code works well.
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<?php
$xml = new DOMDocument('1.0', 'utf-8');
$xml->formatOutput = true;
$xml->preserveWhiteSpace = false;
$xml->load('examples.xml');
//Get item Element
$element = $xml->getElementsByTagName('person')->item(0);
//Load child elements
$name = $element->getElementsByTagName('name')->item(0);
$comment = $element->getElementsByTagName('comment')->item(0) ;
//Replace old elements with new
$element->replaceChild($name, $name);
$element->replaceChild($comment, $comment);
?>
<?php
if (isset($_POST['submit']))
{
$name->nodeValue = $_POST['namanya'];
$comment->nodeValue = $_POST['commentnya'];
htmlentities($xml->save('examples.xml'));
}
?>
<form method="POST" action=''>
name <input type="text-name" value="<?php echo $name->nodeValue ?>" name="namanya" />
comment <input type="text-comment" value="<?php echo $comment->nodeValue ?>" name="commentnya"/>
<input name="submit" type="submit" />
</form>
How to modify a XML node by attribute
When you use XPath as you say it returns an array. As this is the first item you want to change you use [0]
.
To update the value, you have to get SimpleXML to know you want to set the value of the element, the simplest way of doing this is to use (in this case) is $foo[0]
. Although $foo
isn't an array, it fools SimpleXML into setting the value of the element rather than assigning a value to the variable called $foo
.
$xmlStr = '<?xml version="1.0" encoding="utf-8"?>
<players>
<string name="Paul">Foo</string>
<string name="Peter">Bar</string>
</players>';
$xml = new SimpleXMLElement($xmlStr);
$foo = $xml->xpath('//string[@name="Paul"]')[0];
$foo[0] = 'Baobab';
echo $xml->asXML();
If you knew this was always going to be the layout of the XML, you could just do...
$xml->string[0] = 'Baobab';
How to change xml node attribute value with php?
You don't really nee foreach
if you have only one target user. Try changing
foreach( $xml->Users->xpath("User [@Name='user1']") as $t ) {
$t->xpath("Option[@Name='Pass']") = '654321';
}
to
$target = $xml->xpath('//User[@Name="user1"]/Option[@Name="Pass"]')[0];
$target[0]="654321";
echo($xml->asXml());
and see if it works.
How to update XML element for a particular node using php?
You are already half way there. Do it like this:
$xml = simplexml_load_string($x); // assume XML in $x
$update = $xml->xpath("//BlOCK[@ID='G7']")[0]; // requires PHP >= 5.4
$update['NAME'] = "Michi";
If you look at $xml
now, the NAME
-attribute of the <BlOCK>
-node with ID='G7'
is changed.
see it working: http://codepad.viper-7.com/F9Pte1
PHP Change XML node values
The ProcessTransaction
element (and all of its child nodes) are in the "http://example.com" namespace. If you want to access them using xpath()
, you'll need to register a namespace prefix:
$xml->registerXPathNamespace('ex', 'http://example.com');
You can then use the ex
prefix on all relevant parts of your query
$result = $xml->xpath("/soap:Envelope/soap:Body/ex:ProcessTransaction/ex:TransactionRequest/ex:Header/ex:MerchantInfo");
The rest of your code should function correctly, see https://eval.in/916856
Change XML node element value in PHP and save file
You can use XPath to find the specific element. SimpleXMLElement->xpath() returns an array of (matching) SimpleXMLElement objects, i.e. you can access and change the data of each element just like you would in "your" foreach loop.
<?php
// $testimonials = simplexml_load_file('test.xml');
$testimonials = new SimpleXMLElement('<testimonials>
<testimonial id="4c050652f0c3e">
<nimi>John</nimi>
<email>test@test.com</email>
<text>Some text</text>
<active>1</active>
</testimonial>
<testimonial id="4c05085e1cd4f">
<name>ats</name>
<email>some@test.ee</email>
<text>Great site!</text>
<active>0</active>
</testimonial>
</testimonials>');
// there can be only one item with a specific id, but foreach doesn't hurt here
foreach( $testimonials->xpath("testimonial[@id='4c05085e1cd4f']") as $t ) {
$t->name = 'LALALA';
}
echo $testimonials->asXML();
// $testimonials->asXML('test.xml');
prints
<?xml version="1.0"?>
<testimonials>
<testimonial id="4c050652f0c3e">
<nimi>John</nimi>
<email>test@test.com</email>
<text>Some text</text>
<active>1</active>
</testimonial>
<testimonial id="4c05085e1cd4f">
<name>LALALA</name>
<email>some@test.ee</email>
<text>Great site!</text>
<active>0</active>
</testimonial>
</testimonials>
Related Topics
PHP Is Not Recognized as an Internal or External Command in Command Prompt
Unit Testing and Static Methods
Asynchronous Function Call in PHP
What Security Issues Should I Look Out for in PHP
Regex to Detect Invalid Utf-8 String
How to Hide the Actual Download Folder Location
How to Get File Url Using Storage Facade in Laravel 5
PHP Errors Not Being Displayed in the Browser [Ubuntu 10.10]
How to Count the Numbers of Rows That a MySQL Query Returned
Request Headers Bag Is Missing Authorization Header in Symfony 2
PHP Regular Expression for Strong Password Validation
PHP 5.4 - 'Closure $This Support'
PHP Flush That Works... Even in Nginx
How to Force Laravel Project to Use Https for All Routes
PHP Regex Delimiter, What's the Point