-2

Hi I need to modify xml using php from exsiting xml content existing xml

<?xml version="1.0" encoding="utf-8"?>
<Book  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" BookId="10010">
     <STRING id="Name" >Test1</STRING>
    <STRING id="ISBN">102399</STRING>
</Book>

new xml

<?xml version="1.0" encoding="utf-8"?>
<Book BookId="10010" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
     <STRING id="Name" >XYZ</STRING>
    <STRING id="ISBN">7777</STRING>
</Book>

Note: BookID should be place before xmlns attribute

Thank you in advance.

Prakash
  • 813
  • 1
  • 8
  • 20

1 Answers1

0

Use SimpleXML:

$xml = simplexml_load_file($yourXMLPath);

//Loop for element...
foreach ($xml->STRING as $string) {
    //Update value for <STRING id="Name">...
    if($string->attributes()->id == 'Name'){
        $xml->STRING = 'XYZ';
    }

    //Update value for <STRING id="Name">...
    if($string->attributes()->id == 'ISBN'){
        $xml->STRING = '7777';
    }
}

// save the updated document
$xml->asXML('newXML.xml');
AddWeb Solution Pvt Ltd
  • 21,025
  • 5
  • 26
  • 57