-1

I am trying to edit some XML values via my PHP admin panel what do I have to do?

I have tried DOMDocument from PHP but it didn't help me ( I AM USING PHP 5 )

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<tns:database xmlns:tns="http://www.iw.com/sns/platform/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <ItemSpec id="57000" type="1" rewardid="232900" function_on="1" popup="1"/>
</tns:database>

Let's say I want to edit the rewardid value for id="57000" but I couldn't do it

1 Answers1

0

Assuming a string source for the XML rather than a file ( though it is just as easy to do the following )

$xml='<?xml version="1.0" encoding="UTF-8" standalone="no"?>
        <tns:database xmlns:tns="http://www.iw.com/sns/platform/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
          <ItemSpec id="57000" type="1" rewardid="232900" function_on="1" popup="1"/>
        </tns:database>';

$dom=new DOMDocument;
$dom->loadXML( $xml );

$xp=new DOMXPath( $dom );
$col=$xp->query('//ItemSpec[@id="57000"]');

if( $col->length > 0 ){
    $attr=$dom->createAttribute('rewardid');
    $attr->nodeValue='banana';

    $node=$col->item(0);
    $node->removeAttribute('rewardid');
    $node->appendChild( $attr );
}
echo $dom->saveXML();

If it is a file source then $dom->load( $file ) and at the end $dom->save( $file ) etc

To edit an existing attribute,rather than create a new one as above and append it you can simply do:

$node->setAttribute('rewardid','banana'); //etc
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46
  • `setAttribute()` will create an attribute if it does not exists. So you can simplify the source to: https://3v4l.org/UZq4H – ThW Sep 18 '19 at 15:34