<?php
$doc = new DOMDocument('1.0','utf-8');
$doc->formatoutput = true;
$root = $doc->createElement('root');
$index = $doc->createElement('index');
$id = $doc->createAttribute('id');
$newsid = $doc->createTextNode('2');
$newsco = $doc->createTextNode('infoconent');
$id->appendChild($newsid);
$index->appendChild($id);
$index->appendChild($newsco);
$root->appendChild($index);
$doc->appendChild($root);
//这里加入了一个判断,如果有此XML文件则删除后在生成一个
if (file_exists('demo.xml')){
unlink('demo.xml');
$doc->save('demo.xml');
}else{
$doc->save('demo.xml');
}
?>