php中DOMDocument简单用法示例代码(XML创建、添加、删除、修改)
程序员文章站
2022-04-09 10:07:19
共分四个文件,分别是创建、增加、删除、修改四个功能,变量都是写死的,改一改用$_post方式接收就可以用了 //index.php 创建功能 复制代码 代码如下: <...
共分四个文件,分别是创建、增加、删除、修改四个功能,变量都是写死的,改一改用$_post方式接收就可以用了
//index.php 创建功能
<?php
$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');//新建节点
jb51.net$index = $doc -> createelement('index');//新建节点
jb51.net$url = $doc -> createattribute('url');//新建属性
$patch = $doc -> createtextnode($_htmlpatch);//新建text值
$url -> appendchild($patch);//将$patch文本设为$url属性的值
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);//将$id设为index节点的属性,以下类同
$index -> appendchild($title);
$index -> appendchild($content);
$index -> appendchild($url);
$index -> appendchild($author);
$index -> appendchild($sendtime);
jb51.net$root -> appendchild($index);//设置index为root字节点
jb51.net$doc -> appendchild($root);//设置root为跟节点
jb51.net$doc -> save($xmlpatch);//保存文件
jb51.netecho $xmlpatch . ' has create success';
jb51.net?>
jb51.net<!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>
jb51.net<body>
</body>
</html>
//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';
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!';
}
?>
<!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>
jb51.net<body>
</body>
</html>
//edit.php 修改功能(这里只修改title属性值 跟节点值)
<?php
$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?>
<!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>
jb51.net<body>
</body>
</html>
//del.php 删除功能
<?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?>
<!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>
jb51.net<body>
</body>
</html>
jb51.net
总结一下,创建跟添加主要用的就是create跟appendchild,create后边跟element就是创建节点,跟attribute就是创建属性,textnode就是创建值,然后appendchild就是设置从属关系,这么一看非常简单。删除与修改都是用先获得节点列表getelementsbytagname然后foreach遍历想要修改的节点.
//index.php 创建功能
复制代码 代码如下:
<?php
$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');//新建节点
jb51.net$index = $doc -> createelement('index');//新建节点
jb51.net$url = $doc -> createattribute('url');//新建属性
$patch = $doc -> createtextnode($_htmlpatch);//新建text值
$url -> appendchild($patch);//将$patch文本设为$url属性的值
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);//将$id设为index节点的属性,以下类同
$index -> appendchild($title);
$index -> appendchild($content);
$index -> appendchild($url);
$index -> appendchild($author);
$index -> appendchild($sendtime);
jb51.net$root -> appendchild($index);//设置index为root字节点
jb51.net$doc -> appendchild($root);//设置root为跟节点
jb51.net$doc -> save($xmlpatch);//保存文件
jb51.netecho $xmlpatch . ' has create success';
jb51.net?>
jb51.net<!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>
jb51.net<body>
</body>
</html>
//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';
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!';
}
?>
<!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>
jb51.net<body>
</body>
</html>
//edit.php 修改功能(这里只修改title属性值 跟节点值)
复制代码 代码如下:
<?php
$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?>
<!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>
jb51.net<body>
</body>
</html>
//del.php 删除功能
复制代码 代码如下:
<?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?>
<!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>
jb51.net<body>
</body>
</html>
jb51.net
总结一下,创建跟添加主要用的就是create跟appendchild,create后边跟element就是创建节点,跟attribute就是创建属性,textnode就是创建值,然后appendchild就是设置从属关系,这么一看非常简单。删除与修改都是用先获得节点列表getelementsbytagname然后foreach遍历想要修改的节点.
上一篇: PHP中for与foreach的区别分析
下一篇: 只需简单7步,创业者寻找域名无烦恼