您好,欢迎访问一九零五行业门户网

php DOMDocument应用实例(XML创建、添加、删除、修改)

formatoutput = true;$root = $doc -> createelement_x('root');//新建节点$index = $doc -> createelement_x('index');//新建节点$url = $doc -> createattribute('url');//新建属性$patch = $doc -> createtextnode($_htmlpatch);//新建text值$url -> appendchild($patch);//将$patch文本设为$url属性的值$id = $doc -> createattribute('id');$newsid = $doc -> createtextnode($_id);$id -> appendchild($newsid);$title = $doc -> createattribute('title');$newstitle = $doc -> createtextnode($_title);$title -> appendchild($newstitle);$content = $doc -> createtextnode($_content);//节点值$author = $doc -> createattribute('author');$newsauthor = $doc -> createtextnode($_author);$author -> appendchild($newsauthor);$sendtime = $doc -> createattribute('time');$newssendtime = $doc -> createtextnode($_sendtime);$sendtime -> appendchild($newssendtime);$index -> appendchild($id);//将$id设为index节点的属性,以下类同$index -> appendchild($title);$index -> appendchild($content);$index -> appendchild($url);$index -> appendchild($author);$index -> appendchild($sendtime);$root -> appendchild($index);//设置index为root字节点$doc -> appendchild($root);//设置root为跟节点$doc -> save($xmlpatch);//保存文件echo $xmlpatch . ' has create success';?>xml操作
复制代码
2、add.php 增加功能(跟index.php文件差不多,主要就是加个load载入跟 $root = $doc -> documentelement获得跟节点
formatoutput = true;if($doc -> load($xmlpatch)) {$root = $doc -> documentelement;//获得根节点(root)$index = $doc -> createelement_x('index');$url = $doc -> createattribute('url');$patch = $doc -> createtextnode($_htmlpatch);$url -> appendchild($patch);$id = $doc -> createattribute('id');$newsid = $doc -> createtextnode($_id);$id -> appendchild($newsid);$title = $doc -> createattribute('title');$newstitle = $doc -> createtextnode($_title);$title -> appendchild($newstitle);$content = $doc -> createtextnode($_content);$author = $doc -> createattribute('author');$newsauthor = $doc -> createtextnode($_author);$author -> appendchild($newsauthor);$sendtime = $doc -> createattribute('time');$newssendtime = $doc -> createtextnode($_sendtime);$sendtime -> appendchild($newssendtime);$index -> appendchild($id);$index -> appendchild($title);$index -> appendchild($content);$index -> appendchild($url);$index -> appendchild($author);$index -> appendchild($sendtime);$root -> appendchild($index);$doc -> save($xmlpatch);echo $_id . ' has been added in ' . $xmlpatch;} else {echo 'xml file loaded error!';}?>xml操作-添加
复制代码
3edit.php 修改功能(只修改title属性值 跟节点值) 1 2 下一页 尾页
其它类似信息

推荐信息