php 操作xml文件
读取xml:
$doc=new DOMDocument(); $doc->load('news.xml'); $news=$doc->getElementsByTagName("news"); for($i=0;$i<$news->length;$i++){ $item=$news->item($i); }
修改xml :
$doc=new DOMDocument(); $doc->load('news.xml'); $xinwen=$doc->documentElement; $id=1; $newsinfo=$doc->getElementsByTagName("news"); if($newsinfo->length>0){ $item=$newsinfo->item($newsinfo->length-1); $id=intval($item->getElementsByTagName("news_id")->item(0)->nodeValue)+1; } $news= $doc->createElement('news'); $xinwen->appendChild($news); $newsid=$doc->createElement('news_id'); $news->appendChild($newsid); $newid_val=$doc->createTextNode($id); $newsid->appendChild($newid_val); $newtitle=$doc->createElement('news_title'); $news->appendChild($newtitle); $newtitle_val=$doc->createTextNode($news_title); $newtitle->appendChild($newtitle_val); $newcontent=$doc->createElement("news_content"); $news->appendChild($newcontent); $newcontent_val=$doc->createTextNode($news_content); $newcontent->appendChild($newcontent_val); $newadddate=$doc->createElement("news_adddate"); $news->appendChild($newadddate); $newadddate_val=$doc->createTextNode($news_adddate); $newadddate->appendChild($newadddate_val); $xfontfacetitle=$doc->createElement("fontfacetitle"); $news->appendChild($xfontfacetitle); $xfontfamilytitle = $doc->createElement("fontfamilytitle"); $news->appendChild($xfontfamilytitle); $xfontfacecontent =$doc->createElement("fontfacecontent"); $news->appendChild($xfontfacecontent); $xfontfamilycontent =$doc->createElement("fontfamilycontent"); $news->appendChild($xfontfamilycontent); $xfontfaceadddate =$doc->createElement("fontfaceadddate"); $news->appendChild($xfontfaceadddate); $xfontfamilyadddate =$doc->createElement("fontfamilyadddate"); $news->appendChild($xfontfamilyadddate); $arr=$res->FontfaceList; $fp=fopen('news.xml',"w"); fwrite($fp,$doc->saveXML());