php 在原有xml文件中追加节点

来源:互联网 发布:淘宝店铺广告联盟 编辑:程序博客网 时间:2024/04/30 12:24
<?php
$xmlpatch = 'index.xml';


$doc = new DOMDocument(); 
$doc -> formatOutput = true; 
if($doc -> load($xmlpatch)) { 
$document = $doc -> documentElement;//获得根节点(root) 


for($i=1;$i<10;$i++){
$item=$doc->createElement("item");  
$item=$document->appendChild($item);

$date1=$doc->createAttribute("id");
$date1=$item->appendChild($date1);
$date1->appendChild($doc->createTextNode($i));

//操作
$webName=$doc->createElement("op");  
$webName=$item->appendChild($webName);    
$webName->appendChild($doc->createTextNode("add"));

$url = $doc -> createAttribute('url');//新建属性
$patch = $doc -> createTextNode("http://domain/".$i);//新建TEXT值
$url -> appendChild($patch);//将$patch文本设为$url属性的值
$webName->appendChild($url);

$id = $doc -> createAttribute('id');
$newsid = $doc -> createTextNode($i);
$id -> appendChild($newsid);
$webName->appendChild($id);

//影片质量
$webName=$doc->createElement("att");  
$webName=$item->appendChild($webName);    
$webName->appendChild($doc->createTextNode("hd"));

$title = $doc -> createAttribute('title');
$newstitle = $doc -> createTextNode('title'.$i);
$title -> appendChild($newstitle);
$sendtime = $doc -> createAttribute('time');
$newssendtime = $doc -> createTextNode(time());
$sendtime -> appendChild($newssendtime);
$webName->appendChild($title);
$webName->appendChild($sendtime);

//片名
$webName=$doc->createElement("title");  
$webName=$item->appendChild($webName);    
$webName->appendChild($doc->createCDATASection('title'.$i));

$author = $doc -> createAttribute('author');
$newsauthor = $doc -> createTextNode('author'.$i);
$author -> appendChild($newsauthor);
$webName->appendChild($author);

//dc:creator
$creator = $doc->createElement("dc:creator");
$creator = $item->appendChild($creator);
$creator->appendChild($doc->createCDATASection('dcauthor'.$i));

$document->appendChild($item);
}
}


$doc -> save($xmlpatch);//保存文件


echo $xmlpatch . ' has add success';


?>
0 0
原创粉丝点击