xml file: stu.xml:
Copy the codeThe code is as follows:
Zhang San
< yuwen>80
90
70
李思 name>
60
90
75
The above file stu.xml provides some student data.
Now you need to add a zongfen attribute to each xuesheng node and its value is equal to the value of yuwen+shuxue+yingyu
as shown below:
Copy the codeThe code is as follows:
Zhang San
< yuwen>80
90
70
240
& lt;xuesheng>
< ;name>李思
60
90
75
2225
Use php to implement:
Copy the codeThe code is as follows:
$doc = new DOMDocument();
$doc->load( 'stu.xml' );
$students = $doc->getElementsByTagName( “xuesheng” );
foreach($students as $stu){
$yuwen = $stu->getElementsByTagName( “yuwen” )->item(0)->nodeValue;
$shuxue = $stu->getElementsByTagName( “shuxue” )-> item(0)->nodeValue;
$yingyu = $stu->getElementsByTagName( “yingyu” )->item(0)->nodeValue;
$zongfen = $stu->getElementsByTagName( “zongfen” )->item(0)->nodeValue;
if($zongfen){
echo "The node already exists, no more adding!";
continue;
}
$zongfen = $yuwen+$shuxue+$yingyu;
$newNode = $doc->createElement("zongfen",$zongfen);
$stu->appendChild($newNode);
}
$result = $doc->saveXML('stu.xml') ;
?>