Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change XML node element value in PHP and save file

Tags:

dom

php

xml

<testimonials>
    <testimonial id="4c050652f0c3e">
        <nimi>John</nimi>
        <email>[email protected]</email>
        <text>Some text</text>
        <active>1</active>
        </testimonial>
    <testimonial id="4c05085e1cd4f">
        <name>ats</name>
        <email>[email protected]</email>
        <text>Great site!</text>
        <active>0</akctive>
    </testimonial>
</testimonials>

I have this XML strcuture and i need to find a testimonial with specific id and change its value and save file. I have a PHP script deleting specific testimonial according its ID:

<?php
$xmlFile = file_get_contents('test.xml');
$xml = new SimpleXMLElement($xmlFile);

$kust_id = $_GET["id"];

foreach($xml->testimonial as $story) {
    if($story['id'] == $kust_id) {
        $dom=dom_import_simplexml($story);
        $dom->parentNode->removeChild($dom);

        $xml->asXML('test.xml');
        header("Location: newfile.php");
    }
}
?>
like image 229
Hannes Avatar asked Jun 02 '10 10:06

Hannes


People also ask

How do I change the value of a node in XML?

Change the Value of an Attribute In the DOM, attributes are nodes. Unlike element nodes, attribute nodes have text values. 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 the difference between element and node in XML?

An Element is part of the formal definition of a well-formed XML document, whereas a node is defined as part of the Document Object Model for processing XML documents.

Can I write PHP code in XML file?

If you run the XML file as a PHP script, then yes.

What is node value in XML?

Element nodes do not have a text value. The text value of an element node is stored in a child node. This node is called a text node. To retrieve the text value of an element, you must retrieve the value of the elements' text node.


1 Answers

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>[email protected]</email>
        <text>Some text</text>
        <active>1</active>
        </testimonial>
    <testimonial id="4c05085e1cd4f">
        <name>ats</name>
        <email>[email protected]</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>[email protected]</email>
        <text>Some text</text>
        <active>1</active>
        </testimonial>
    <testimonial id="4c05085e1cd4f">
        <name>LALALA</name>
        <email>[email protected]</email>
        <text>Great site!</text>
        <active>0</active>
    </testimonial>
</testimonials>
like image 176
VolkerK Avatar answered Oct 27 '22 08:10

VolkerK