Overriding an XML File

Asked 2 years ago, Updated 2 years ago, 98 views

<Alert>
   name = 'mmm' 
    hope='kk'
    <test>
       Name = 'kkm' 
     </test>
</Alert>
$str="test";
$fileName="test-fileName";
$xml = simplexml_load_file(sampleXml);
(String) $xml->Alert->attribute()->hope=$str;
$xml->saveXML($fileName);

I can get elements, but I can't rewrite them...
Don't you know a good way?

Permission permissions can be written and read.

php xml

2022-09-30 16:26

1 Answers

XML does not look good, but if you want to rewrite the hop value in this format, you need to get the text of Alert, replace the string, or parse the key value format separately.

If you want to rewrite it a little more XML-like, for example, you can rewrite hop with the following code:

<?php
    $str = 'test';
    $fileName='./new.xml';
    $xml = simplexml_load_file('./sample.xml');
    (String) $xml->alert->attributes()->hope=$str;
    $xml->saveXML($fileName);
?>
<?xml version="1.0"?>
<root>
  <alert hop="kk">
  </alert>
</root>


2022-09-30 16:26

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.