php中DOMDocument简单用法示例代码(XML创建、添加、删除、修改)
发布时间:2020-12-13 05:48:51 所属栏目:PHP教程 来源:网络整理
导读:共分四个文件,分别是创建、增加、删除、修改四个功能,变量都是写死的,改一改用$_POST方式接收就可以用了 //index.php 创建功能 div class="codetitle" a style="CURSOR: pointer" data="52672" class="copybut" id="copybut52672" onclick="doCopy('code5
共分四个文件,分别是创建、增加、删除、修改四个功能,变量都是写死的,改一改用$_POST方式接收就可以用了 //index.php 创建功能 <div class="codetitle"><a style="CURSOR: pointer" data="52672" class="copybut" id="copybut52672" onclick="doCopy('code52672')"> 代码如下:<div class="codebody" id="code52672"> <?php $xmlpatch = 'index.xml'; $_id = '1'; $_title = 'title1'; $_content = 'content1'; $_author = 'author1'; $_sendtime = 'time1'; $_htmlpatch = '1.html'; 52php.cn$doc = new DOMDocument('1.0','utf-8'); $doc -> formatOutput = true; 52php.cn$root = $doc -> createElement('root');//新建节点 52php.cn$index = $doc -> createElement('index');//新建节点 52php.cn$url = $doc -> createAttribute('url');//新建属性 $patch = $doc -> createTextNode($_htmlpatch);//新建TEXT值 $url -> appendChild($patch);//将$patch文本设为$url属性的值 52php.cn$id = $doc -> createAttribute('id'); $newsid = $doc -> createTextNode($_id); $id -> appendChild($newsid); 52php.cn$title = $doc -> createAttribute('title'); $newstitle = $doc -> createTextNode($_title); $title -> appendChild($newstitle); 52php.cn$content = $doc -> createTextNode($_content);//节点值 52php.cn$author = $doc -> createAttribute('author'); $newsauthor = $doc -> createTextNode($_author); $author -> appendChild($newsauthor); 52php.cn$sendtime = $doc -> createAttribute('time'); $newssendtime = $doc -> createTextNode($_sendtime); $sendtime -> appendChild($newssendtime); 52php.cn$index -> appendChild($id);//将$id设为index节点的属性,以下类同 $index -> appendChild($title); $index -> appendChild($content); $index -> appendChild($url); $index -> appendChild($author); $index -> appendChild($sendtime); 52php.cn$root -> appendChild($index);//设置index为root字节点 52php.cn$doc -> appendChild($root);//设置root为跟节点 52php.cn$doc -> save($xmlpatch);//保存文件 52php.cnecho $xmlpatch . ' has create success'; 52php.cn?> 52php.cn<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> XML操作 52php.cn
|