Charan Putrevu
Charan Putrevu

Reputation: 84

Update data in XML file using PHP

Actually I am working on a website for which I manage the database using XML file(s). I want to update the XML file which means I want to change the text in between tags of XML file. For example my XML file structure is as follows

<books>
<book>
<Name>BookName1</Name>
<Author>author1</Author>
</book>
<book>
<Name>BookName2</Name>
<Author>author2</Author>
</book>
</books>

Now what I want to do is update the BookName1 in the Name tag to BookName2. Any tag at any node likewise. I want to do it with PHP.

Upvotes: 0

Views: 338

Answers (1)

Jan
Jan

Reputation: 43169

Have a look at SimpleXML. You can access and change your elements like an array then:

<?php 
$string = '<books>
<book>
<Name>BookName1</Name>
<Author>author1</Author>
</book>
<book>
<Name>BookName2</Name>
<Author>author2</Author>
</book>
</books>';

$xml = simplexml_load_string($string);
$xml->book[0]->Name= "Something else"; // or BookName2
echo $xml->asXML();
?>

Upvotes: 1

Related Questions