教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 php DOMDocument应用实例(XML创建、添加、删除、修改)

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

发布时间:2015-08-25   编辑:jiaochengji.com
php DOMDocument的用法,主要介绍使用DOMDocument进行xml文件的几个操作,供大家学习参考。

php DOMDocument的用法,这里主要介绍使用DOMDocument进行xml文件的几个操作,供大家学习参考。

1、index.php 创建功能
 

复制代码 代码示例:
<?php
$xmlpatch = 'index.xml';
$_id = '1';
$_title = 'title1';
$_content = 'content1';
$_author = 'author1';
$_sendtime = 'time1';
$_htmlpatch = '1.html';
$doc = new DOMDocument('1.0', 'utf-8');
$doc -> 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';
?>
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>XML操作</title>
</head>
<body>
</body>
</html>

 

复制代码 代码示例:

2、add.php 增加功能(跟index.php文件差不多,主要就是加个load载入跟 $root = $doc -> documentElement获得跟节点
<?php
$xmlpatch = 'index.xml';
$_id = '2';
$_title = 'title2';
$_content = 'content2';
$_author = 'author2';
$_sendtime = 'time2';
$_htmlpatch = '2.html';
$doc = new DOMDocument();
$doc -> 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!';
}
?>
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>XML操作-添加</title>
</head>
<body>
</body>
</html>

3edit.php 修改功能(只修改title属性值 跟节点值)

您可能感兴趣的文章:
php DOMDocument应用实例(XML创建、添加、删除、修改)
【PHP】xml学习笔记之增删改查(二)删、改、查
【PHP】xml学习笔记之增删改查(一)增加
php DOMElement操作xml文档的例子
实例学习PHP操作XML的类DOMDocument
php解析XML数据的一段代码
php操作xml的各种方法解析
php生成xml简单实例详解
php解析XML文档属性并编辑的代码
php操作xml的实例代码

[关闭]
~ ~