「PHP」開発–XMLファイルに要素を追加する方法

phpコード
$doc = new DOMDocument();
$doc->load( ‘kdfc.xml’ );
$students = $doc->getElementsByTagName( “student” );
foreach($students as $stu){
$computer = $stu->getElementsByTagName( “computer” )->item(0)->nodeValue;
$math = $stu->getElementsByTagName( “math” )->item(0)->nodeValue;
$english = $stu->getElementsByTagName( “english” )->item(0)->nodeValue;
$total = $stu->getElementsByTagName( “total” )->item(0)->nodeValue;
if($total){
echo “ノードが既に存在しますから追加されません”;
continue;
}
$total = $computer+$math+$english;
$newNode = $doc->createElement(“total”,$total);
$stu->appendChild($newNode);
}
$result = $doc->saveXML(‘kdfc.xml’);

PHP

Posted by arkgame