最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2010年12月19日 21:16:22   作者:  
PHP寫XML方法很多,這里主要介紹一下DOMDocument的用法,跟 JS大體上相同,其實(shí)非常簡(jiǎn)單。
共分四個(gè)文件,分別是創(chuàng)建、增加、刪除、修改四個(gè)功能,變量都是寫死的,改一改用$_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é)點(diǎn)
jb51.net$index = $doc -> createElement('index');//新建節(jié)點(diǎn)
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é)點(diǎn)值
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é)點(diǎn)的屬性,以下類同
$index -> appendChild($title);
$index -> appendChild($content);
$index -> appendChild($url);
$index -> appendChild($author);
$index -> appendChild($sendtime);
jb51.net$root -> appendChild($index);//設(shè)置index為root字節(jié)點(diǎn)
jb51.net$doc -> appendChild($root);//設(shè)置root為跟節(jié)點(diǎn)
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文件差不多,主要就是加個(gè)load載入跟 $root = $doc -> documentElement獲得跟節(jié)點(diǎn)
復(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é)點(diǎn)(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é)點(diǎn)值)
復(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é)點(diǎn)值,真是太意外了,沒想到跟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é)點(diǎn),跟Attribute就是創(chuàng)建屬性,TextNode就是創(chuàng)建值,然后appendChild就是設(shè)置從屬關(guān)系,這么一看非常簡(jiǎn)單。刪除與修改都是用先獲得節(jié)點(diǎn)列表getElementsByTagName然后foreach遍歷想要修改的節(jié)點(diǎn).

相關(guān)文章

最新評(píng)論

修武县| 林甸县| 新宁县| 体育| 台中市| 唐河县| 宁蒗| 墨竹工卡县| 垣曲县| 郑州市| 修武县| 开鲁县| 蓬溪县| 河南省| 邯郸市| 揭阳市| 长海县| 白水县| 峡江县| 陆丰市| 抚顺县| 新邵县| 乌鲁木齐县| 临高县| 广宗县| 军事| 建平县| 绥棱县| 蓬莱市| 习水县| 连平县| 绵阳市| 东平县| 偏关县| 延庆县| 灵石县| 沙河市| 华坪县| 台安县| 贵州省| 蚌埠市|