0

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.

Jan
  • 42,290
  • 8
  • 54
  • 79
  • 2
    Possible duplicate of [How to update the XML file](http://stackoverflow.com/questions/23609422/how-to-update-the-xml-file) – michi Jan 20 '16 at 12:53
  • minimal research effort: http://stackoverflow.com/questions/4906073/a-simple-program-to-crud-node-and-node-values-of-xml-file/4906459#4906459 – michi Jan 20 '16 at 12:54

1 Answers1

1

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();
?>
Jan
  • 42,290
  • 8
  • 54
  • 79