Simple usage example code of DOMDocument in php (XML creation, addition, deletion, modification)_PHP tutorial

WBOY
Release: 2016-07-21 15:32:44
Original
803 people have browsed it

It is divided into four files, which have four functions: create, add, delete and modify. The variables are all hard-coded. You can use $_POST instead to receive them
//index.php creation function

Copy code The code is as follows:

$xmlpatch = 'index.xml';
$ _id = '1';
$_title = 'title1';
$_content = 'content1';
$_author = 'author1';
$_sendtime = 'time1';
$ _htmlpatch = '1.html';
jb51.net$doc = new DOMDocument('1.0', 'utf-8');
$doc -> formatOutput = true;
jb51.net$ root = $doc -> createElement('root');//New node
jb51.net$index = $doc -> createElement('index');//New node
jb51.net$ url = $doc -> createAttribute('url');//New attribute
$patch = $doc -> createTextNode($_htmlpatch);//New TEXT value
$url -> appendChild( $patch);//Set the $patch text to the value of the $url attribute
jb51.net$id = $doc -> createAttribute('id');
$newsid = $doc -> createTextNode ($_id);
$id -> appendChild($newsid);
jb51.net$title = $doc -> createAttribute('title');
$newstitle = $doc -> ; createTextNode($_title);
$title -> appendChild($newstitle);
jb51.net$content = $doc -> createTextNode($_content);//Node value
jb51. net$author = $doc -> createAttribute('author');
$newsauthor = $doc -> createTextNode($_author);
$author -> appendChild($newsauthor);
jb51.net$sendtime = $doc -> createAttribute('time');
$newssendtime = $doc -> createTextNode($_sendtime);
$sendtime -> appendChild($newssendtime);
jb51.net$index -> appendChild($id);//Set $id as the attribute of the index node, the following is similar to
$index -> appendChild($title);
$index -> appendChild($content);
$index -> appendChild($url);
$index -> appendChild($author);
$index -> appendChild($sendtime) ;
jb51.net$root -> appendChild($index);//Set index as the root byte point
jb51.net$doc -> appendChild($root);//Set root as the follow Node
jb51.net$doc -> save($xmlpatch);//Save the file
jb51.netecho $xmlpatch . ' has create success';
jb51.net?>
jb51 .net



XML operation

jb51.net



//add.php adds functions (similar to the index.php file, mainly adding a load and $root = $doc -> documentElement to get the following Node
Copy code The code is as follows:

$xmlpatch = 'index.xml';
$_id = '2';
$_title = 'title2';
$_content = 'content2';
$_author = 'author2';
$_sendtime = 'time2';
$_htmlpatch = '2.html';
jb51.net$doc = new DOMDocument();
$doc -> formatOutput = true;
if($doc -> load($xmlpatch)) {
$root = $doc -> documentElement;//获得根节点(root)
$index = $doc -> createElement('index');
jb51.net$url = $doc -> createAttribute('url');
$patch = $doc -> createTextNode($_htmlpatch);
$url -> appendChild($patch);
jb51.net$id = $doc -> createAttribute('id');
$newsid = $doc -> createTextNode($_id);
$id -> appendChild($newsid);
jb51.net$title = $doc -> createAttribute('title');
$newstitle = $doc -> createTextNode($_title);
$title -> appendChild($newstitle);
jb51.net$content = $doc -> createTextNode($_content);
jb51.net$author = $doc -> createAttribute('author');
$newsauthor = $doc -> createTextNode($_author);
$author -> appendChild($newsauthor);
jb51.net$sendtime = $doc -> createAttribute('time');
$newssendtime = $doc -> createTextNode($_sendtime);
$sendtime -> appendChild($newssendtime);
jb51.net$index -> appendChild($id);
$index -> appendChild($title);
$index -> appendChild($content);
$index -> appendChild($url);
$index -> appendChild($author);
$index -> appendChild($sendtime);
jb51.net$root -> appendChild($index);
jb51.net$doc -> save($xmlpatch);
jb51.netecho $_id . ' has been added in ' . $xmlpatch;
jb51.net} else {
echo 'xml file loaded error!';
}
?>




XML操作-添加

jb51.net



//edit.php 修改功能(这里只修改title属性值 跟节点值)
复制代码 代码如下:

$xmlpatch = 'index.xml';
$_id = '2';
$_title = 'has been changed';
$_content = 'has been changed';
jb51.net$doc = new DOMDocument();
$doc -> formatOutput = true;
jb51.netif($doc -> load($xmlpatch)) {
$root = $doc -> documentElement;
$elm = $root -> getElementsByTagName('index');
$checkexist = 0;
foreach ($elm as $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 . ' is not found in ' . $xmlpatch;
} else {
$doc -> save($xmlpatch);
echo $_id . ' has been changed';
}
} else {
echo 'xml file loaded error!';
}
jb51.net?>




XML操作-修改

jb51.net



//del.php 删除功能
复制代码 代码如下:

$xmlpatch = 'index.xml';
$_id = '2';
jb51.net$doc = new DOMDocument();
$doc -> formatOutput = true;
if($doc -> load($xmlpatch)) {
$root = $doc -> documentElement;
$elm = $root -> getElementsByTagName('index');
foreach ($elm as $new) {
if($new -> getAttribute('id') == $_id) {
if($root -> removeChild($new)) {
echo $_id . ' has been deleted';
} else {
echo $_id . ' delete failed';
}
}
}
$doc -> save($xmlpatch);
} else {
echo 'xml file loaded error!';
}
jb51.net?>




XML操作-删除

jb51.net



jb51.net
总结一下,创建跟添加主要用的就是create跟appendChild,create后边跟Element就是创建节点,跟Attribute就是创建属性,TextNode就是创建值,然后appendChild就是设置从属关系,这么一看非常简单。删除与修改都是用先获得节点列表getElementsByTagName然后foreach遍历想要修改的节点.

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/322777.htmlTechArticle共分四个文件,分别是创建、增加、删除、修改四个功能,变量都是写死的,改一改用$_POST方式接收就可以用了 //index.php 创建功能 复制代码...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template