拉风的咖菲猫
假设你的xml为stu.xml---------------那么--------------$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);}echo $result = $doc->saveXML();file_put_contents("stu.xml",$result); //这句其实是不需要加上的- - 但是我也不晓得为什么save不了,就加了这句~