php中DOMDocument简单用法(XML创建、添加、删除、修改)
2013-09-09 15:07
681 查看
PHP写XML方法很多,这里主要介绍一下DOMDocument的用法,跟JS大体上相同,其实非常简单。
共分四个文件,分别是创建、增加、删除、修改四个功能,变量都是写死的,改一改用$_POST方式接收就可以用了
<?php
$xmlpatch='index.xml';
$_id='1';
$_title='title1';
$_content='content1';
$_author='author1';
$_sendtime='time1';
$_htmlpatch='1.html';
$doc=newDOMDocument('1.0','utf-8');
$doc->formatOutput=true;
$root=$doc->createElement('root');//新建节点
$index=$doc->createElement('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.'hascreatesuccess';
?>
//add.php增加功能(跟index.php文件差不多,主要就是加个load载入跟$root=$doc->do//cumentElement获得跟节点
$xmlpatch='index.xml';
$_id='2';
$_title='title2';
$_content='content2';
$_author='author2';
$_sendtime='time2';
$_htmlpatch='2.html';
$doc=newDOMDocument();
$doc->formatOutput=true;
if($doc->load($xmlpatch)){
$root=$doc->documentElement;//获得根节点(root)
$index=$doc->createElement('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.'hasbeenaddedin'.$xmlpatch;
}else{
echo'xmlfileloadederror!';
}
}
//edit.php修改功能(这里只修改title属性值跟节点值)
<?php
$xmlpatch='index.xml';
$_id='2';
$_title='hasbeenchanged';
$_content='hasbeenchanged';
$doc=newDOMDocument();
$doc->formatOutput=true;
if($doc->load($xmlpatch)){
$root=$doc->documentElement;
$elm=$root->getElementsByTagName('index');
$checkexist=0;
foreach($elmas$new){
if($new->getAttribute('id')==$_id){
$new->setAttribute('title',$_title);
$new->nodeValue=$_content;//修改节点值,真是太意外了,没想到跟JS一样直接能赋值...
//$new->removeChild($new->nodevalue);
$checkexist=1;
}
}
if($checkexist==0){
echo$_id.'isnotfoundin'.$xmlpatch;
}else{
$doc->save($xmlpatch);
echo$_id.'hasbeenchanged';
}
}else{
echo'xmlfileloadederror!';
}
?>
//del.php删除功能
<?php
$xmlpatch='index.xml';
$_id='2';
$doc=newDOMDocument();
$doc->formatOutput=true;
if($doc->load($xmlpatch)){
$root=$doc->documentElement;
$elm=$root->getElementsByTagName('index');
foreach($elmas$new){
if($new->getAttribute('id')==$_id){
if($root->removeChild($new)){
echo$_id.'hasbeendeleted';
}else{
echo$_id.'deletefailed';
}
}
}
$doc->save($xmlpatch);
}else{
echo'xmlfileloadederror!';
}
?>
共分四个文件,分别是创建、增加、删除、修改四个功能,变量都是写死的,改一改用$_POST方式接收就可以用了
<?php
$xmlpatch='index.xml';
$_id='2';
$doc=newDOMDocument();
$doc->formatOutput=true;
if($doc->load($xmlpatch)){
$root=$doc->documentElement;
$elm=$root->getElementsByTagName('index');
foreach($elmas$new){
if($new->getAttribute('id')==$_id){
if($root->removeChild($new)){
echo$_id.'hasbeendeleted';
}else{
echo$_id.'deletefailed';
}
}
}
$doc->save($xmlpatch);
}else{
echo'xmlfileloadederror!';
}
?>
相关文章推荐
- php中DOMDocument简单用法(XML创建、添加、删除、修改)
- php中DOMDocument简单用法示例代码(XML创建、添加、删除、修改)
- php中DOMDocument简单用法示例代码(XML创建、添加、删除、修改)
- php中DOMDocument简单用法(XML创建、添加、删除、修改)
- php中DOMDocument简单用法示例代码(XML创建、添加、删除、修改)
- php中DOMDocument简单用法(XML创建、添加、删除、修改)
- php中DOMDocument简单用法(XML创建、添加、删除、修改)
- php中DOMDocument简单用法(XML创建、添加、删除、修改)
- PHP 创建、增加、删除、修改 xml
- 这是一个关于XML文档的操作管理器XMLHelper类,类中包括XML文档的创建,文档节点和属性的读取,添加,修改,删除的方法功能的实现
- C#-数据库访问技术 ado.net——创建 数据库连接类 与 数据库操作方法 以及简单的数据的添加、删除、修改、查看
- [WinForm Asp.Net]XML添加,删除,修改,读取操作类
- C# 添加,修改,删除Xml节点
- php如何连接MYSQL数据库并进行添加、修改、删除操作
- asp.net C#对xml文档创建 修改 删除 查找
- OC 字符串NSString 创建 拼接 插入 修改 删除 查找判断等基础用法
- 手工修改Tomcat的conf/server.xml需要删除并重新创建eclipse的server
- mysql alter 语句用法,添加、修改、删除字段等
- Oracle的学习二:表管理(数据类型、创建/修改表、添加/修改/删除数据、数据查询)
- php5.4数组介绍和基本操作(创建,删除,修改,使用)