<?php
$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 “节点已经存在了,不再添加!”;
continue;
}
$zongfen = $yuwen+$shuxue+$yingyu;
$newNode = $doc->createElement(“zongfen”,$zongfen);
$stu->appendChild($newNode);
}
$result = $doc->saveXML(‘stu.xml’);
?>