php中DOMDocument简单用法示例代码(XML创建、添加、删除、修改)

前端之家收集整理的这篇文章主要介绍了php中DOMDocument简单用法示例代码(XML创建、添加、删除、修改)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

共分四个文件,分别是创建、增加删除修改四个功能,变量都是写死的,改一改用$_POST方式接收就可以用了
//index.PHP 创建功能
<div class="codetitle"><a style="CURSOR: pointer" data="52672" class="copybut" id="copybut52672" onclick="doCopy('code52672')"> 代码如下:

<div class="codebody" id="code52672">
<?PHP
$xmlpatch = 'index.xml';
$_id = '1';
$_title = 'title1';
$_content = 'content1';
$_author = 'author1';
$_sendtime = 'time1';
$_htmlpatch = '1.html';
jb51.cc$doc = new DOMDocument('1.0','utf-8');
$doc -> formatOutput = true;
jb51.cc$root = $doc -> createElement('root');//新建节点
jb51.cc$index = $doc -> createElement('index');//新建节点
jb51.cc$url = $doc -> createAttribute('url');//新建属性
$patch = $doc -> createTextNode($_htmlpatch);//新建TEXT值
$url -> appendChild($patch);//将$patch文本设为$url属性的值
jb51.cc$id = $doc -> createAttribute('id');
$newsid = $doc -> createTextNode($_id);
$id -> appendChild($newsid);
jb51.cc$title = $doc -> createAttribute('title');
$newstitle = $doc -> createTextNode($_title);
$title -> appendChild($newstitle);
jb51.cc$content = $doc -> createTextNode($_content);//节点值
jb51.cc$author = $doc -> createAttribute('author');
$newsauthor = $doc -> createTextNode($_author);
$author -> appendChild($newsauthor);
jb51.cc$sendtime = $doc -> createAttribute('time');
$newssendtime = $doc -> createTextNode($_sendtime);
$sendtime -> appendChild($newssendtime);
jb51.cc$index -> appendChild($id);//将$id设为index节点的属性,以下类同
$index -> appendChild($title);
$index -> appendChild($content);
$index -> appendChild($url);
$index -> appendChild($author);
$index -> appendChild($sendtime);
jb51.cc$root -> appendChild($index);//设置index为root字节点
jb51.cc$doc -> appendChild($root);//设置root为跟节点
jb51.cc$doc -> save($xmlpatch);//保存文件
jb51.ccecho $xmlpatch . ' has create success';
jb51.cc?>
jb51.cc<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;


XML操作

jb51.cc

@H_301_59@

//add.PHP 增加功能(跟index.PHP文件差不多,主要就是加个load载入跟 $root = $doc -> documentElement获得跟节点
<div class="codetitle"><a style="CURSOR: pointer" data="44387" class="copybut" id="copybut44387" onclick="doCopy('code44387')"> 代码如下:
<div class="codebody" id="code44387">
<?PHP
$xmlpatch = 'index.xml';
$_id = '2';
$_title = 'title2';
$_content = 'content2';
$_author = 'author2';
$_sendtime = 'time2';
$_htmlpatch = '2.html';
jb51.cc$doc = new DOMDocument();
$doc -> formatOutput = true;
if($doc -> load($xmlpatch)) {
$root = $doc -> documentElement;//获得根节点(root)
$index = $doc -> createElement('index');
jb51.cc$url = $doc -> createAttribute('url');
$patch = $doc -> createTextNode($_htmlpatch);
$url -> appendChild($patch);
jb51.cc$id = $doc -> createAttribute('id');
$newsid = $doc -> createTextNode($_id);
$id -> appendChild($newsid);
jb51.cc$title = $doc -> createAttribute('title');
$newstitle = $doc -> createTextNode($_title);
$title -> appendChild($newstitle);
jb51.cc$content = $doc -> createTextNode($_content);
jb51.cc$author = $doc -> createAttribute('author');
$newsauthor = $doc -> createTextNode($_author);
$author -> appendChild($newsauthor);
jb51.cc$sendtime = $doc -> createAttribute('time');
$newssendtime = $doc -> createTextNode($_sendtime);
$sendtime -> appendChild($newssendtime);
jb51.cc$index -> appendChild($id);
$index -> appendChild($title);
$index -> appendChild($content);
$index -> appendChild($url);
$index -> appendChild($author);
$index -> appendChild($sendtime);
jb51.cc$root -> appendChild($index);
jb51.cc$doc -> save($xmlpatch);
jb51.ccecho $_id . ' has been added in ' . $xmlpatch;
jb51.cc} 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"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;


XML操作-<a href="https://www.jb51.cc/tag/tianjia/" target="_blank" class="keywords">添加</a>

jb51.cc

@H_301_59@

//edit.PHP 修改功能(这里只修改title属性值 跟节点值)
<div class="codetitle"><a style="CURSOR: pointer" data="22360" class="copybut" id="copybut22360" onclick="doCopy('code22360')"> 代码如下:
<div class="codebody" id="code22360">
<?PHP
$xmlpatch = 'index.xml';
$_id = '2';
$_title = 'has been changed';
$_content = 'has been changed';
jb51.cc$doc = new DOMDocument();
$doc -> formatOutput = true;
jb51.ccif($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.cc?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;


XML操作-<a href="https://www.jb51.cc/tag/xiugai/" target="_blank" class="keywords">修改</a>

jb51.cc

@H_301_59@

//del.PHP 删除功能
<div class="codetitle">@L_403_3@ 代码如下:
<div class="codebody" id="code61881">
<?PHP
$xmlpatch = 'index.xml';
$_id = '2';
jb51.cc$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.cc?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;


XML操作-<a href="https://www.jb51.cc/tag/shanchu/" target="_blank" class="keywords">删除</a>

jb51.cc

@H_301_59@

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

原文链接:https://www.f2er.com/php/28243.html
DOMDocumentphp

猜你在找的PHP相关文章