四虎精品视频-四虎精品成人免费网站-四虎黄色网-四虎国产视频-国产免费91-国产蜜臀97一区二区三区

php中DOMDocument簡單用法示例代碼(XML創(chuàng)建、添加、刪除、修改)

共分四個文件,分別是創(chuàng)建、增加、刪除、修改四個功能,變量都是寫死的,改一改用$_POST方式接收就可以用了
//index.php 創(chuàng)建功能
復(fù)制代碼 代碼如下:
<?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');//新建節(jié)點
jb51.NET$index = $doc -> createElement('index');//新建節(jié)點
jb51.NET$url = $doc -> createAttribute('url');//新建屬性
$patch = $doc -> createTextNode($_htmlpatch);//新建TEXT值
$url -> appendChild($patch);//將$patch文本設(shè)為$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);//節(jié)點值
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設(shè)為index節(jié)點的屬性,以下類同
$index -> appendChild($title);
$index -> appendChild($content);
$index -> appendChild($url);
$index -> appendChild($author);
$index -> appendChild($sendtime);
jb51.NET$root -> appendChild($index);//設(shè)置index為root字節(jié)點
jb51.NET$doc -> appendChild($root);//設(shè)置root為跟節(jié)點
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獲得跟節(jié)點
復(fù)制代碼 代碼如下:
<?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;//獲得根節(jié)點(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屬性值 跟節(jié)點值)
復(fù)制代碼 代碼如下:
<?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;//修改節(jié)點值,真是太意外了,沒想到跟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 刪除功能
復(fù)制代碼 代碼如下:
<?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
總結(jié)一下,創(chuàng)建跟添加主要用的就是create跟appendChild,create后邊跟Element就是創(chuàng)建節(jié)點,跟Attribute就是創(chuàng)建屬性,TextNode就是創(chuàng)建值,然后appendChild就是設(shè)置從屬關(guān)系,這么一看非常簡單。刪除與修改都是用先獲得節(jié)點列表getElementsByTagName然后foreach遍歷想要修改的節(jié)點.

php技術(shù)php中DOMDocument簡單用法示例代碼(XML創(chuàng)建、添加、刪除、修改),轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 贝利亚头像,权威| 小数除法竖式50道带答案| 生死千里| 孕期检查项目一览表| 印度超人3| 战上海老电影战争片子| 《人奶魔劫》电影在线播放| 从此以后歌词| 高志鹏| hellokitty壁纸| 我的漂亮的朋友| 吻胸捏胸揉视频大全 | 第一财经直播电视直播 现场直播| 保镖1993在线观看| 熊出没之过年大电影| av电影网| 胡金铨最好的十部电影| 故乡别来无恙演员表名单| 小数加减法100道题| 徐有容| 陈宝莲拍过的电影| 日日夜精品视频| 庆余年2演员表全部员表| 宇宙魔方| 学籍证明| 追捕演员表| 我落泪情绪零碎周杰伦歌词| 电影《塔蒂亚娜1》演员表| 一个蛋挞的热量| 性欧美18一69性sexhd| 红电视剧演员表| 土壤动植物的乐园教学反思| 欧美一级大胆视频| 出彩中国人第三季 综艺| 袁冰妍个人资料| 离歌吉他谱| 瓦伦蒂诺| 栏目大全| 唐砖演员表| 失落的星球| 麻豆自拍|