您好我需要使用php从现有的xml内容修改xml 现有的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>
注意:BookID应该放在xmlns属性
之前提前谢谢。
答案 0 :(得分:0)
使用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');