php中DOMDocument简单用法示例代码(XML创建、添加、删除、修改)
共分四个文件,分别是创建、增加、删除、修改四个功能,变量都是写死的,改一改用$_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';
jb51.cc$doc = new DOMDocument('1.0','utf-8');
$doc -> formatOutput = true;
jb51.cc$root = $doc -> createElement('root');//新建节点
jb51.cc$index = $doc -> createElement('index');//新建节点
jb51.cc$url = $doc -> createAttribute('url');//新建
属性 $patch = $doc -> createTextNode($_htmlpatch);//新建TEXT值
$url -> appendChild($patch);//将$patch文本设为$url
属性的值
jb51.cc$id = $doc -> createAttribute('id');
$newsid = $doc -> createTextNode($_id);
$id -> appendChild($newsid);
jb51.cc$title = $doc -> createAttribute('title');
$newstitle = $doc -> createTextNode($_title);
$title -> appendChild($newstitle);
jb51.cc$content = $doc -> createTextNode($_content);//节点值
jb51.cc$author = $doc -> createAttribute('author');
$newsauthor = $doc -> createTextNode($_author);
$author -> appendChild($newsauthor);
jb51.cc$sendtime = $doc -> createAttribute('time');
$newssendtime = $doc -> createTextNode($_sendtime);
$sendtime -> appendChild($newssendtime);
jb51.cc$index -> appendChild($id);//将$id设为index节点的
属性,以下类同
$index -> appendChild($title);
$index -> appendChild($content);
$index -> appendChild($url);
$index -> appendChild($author);
$index -> appendChild($sendtime);
jb51.cc$root -> appendChild($index);//设置index为root字节点
jb51.cc$doc -> appendChild($root);//设置root为跟节点
jb51.cc$doc -> save($xmlpatch);//保存
文件 jb51.ccecho $xmlpatch . ' has create success';
jb51.cc?>
jb51.cc<!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操作 jb51.cc
@H_
301_59@
//add.
(跟index.