php 创建xml文件

4 篇文章 0 订阅
<?php
$xmlpatch = 'index.xml';


$doc=new DOMDocument("1.0","utf-8");
$doc->formatOutput=true;               
$document=$doc->createElement("document");
$document=$doc->appendChild($document);


$dc  = $doc->createAttribute("xmlns:dc");
$dc = $document->appendChild($dc);
$dc->appendChild($doc->createTextNode("http://purl.org/dc/elements/1.1/"));


/*
$item = $doc -> createElement('item');//新建节点
$url = $doc -> createAttribute('url');//新建属性
$patch = $doc -> createTextNode('url值');//新建TEXT值
$url -> appendChild($patch);//将$patch文本设为$url属性的值
$item -> appendChild($url);//将$id设为item节点的属性,以下类同


$id = $doc -> createAttribute('id');//新建属性
$newsid = $doc -> createTextNode('id值');//新建TEXT值
$id -> appendChild($newsid);//将$newsid文本设为$id属性的值
$item -> appendChild($id);//将$id设为item节点的属性,以下类同


$title = $doc -> createAttribute('title');
$newstitle = $doc -> createTextNode('title值');
$title -> appendChild($newstitle);
$item -> appendChild($title);


$author = $doc -> createAttribute('author');
$newsauthor = $doc -> createTextNode('author值');
$author -> appendChild($newsauthor);
$item -> appendChild($author);


$sendtime = $doc -> createAttribute('time');
$newssendtime = $doc -> createTextNode('time值');
$sendtime -> appendChild($newssendtime);
$item -> appendChild($sendtime);




$content = $doc -> createTextNode('内容值content');//节点值
$item -> appendChild($content);


$document -> appendChild($item);


$doc -> appendChild($document);


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


echo $xmlpatch . ' has create success';
*/




$webName=$doc->createElement("webName");
$webName=$document->appendChild($webName);    
$webName->appendChild($doc->createCDATASection("名称"));
$webName=$doc->createElement("webSite");  
$webName=$document->appendChild($webName);    
$webName->appendChild($doc->createCDATASection("http://domain/"));


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));

}




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


echo $xmlpatch . ' has create success';


?>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值