亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時(shí)間:2010年12月19日 21:16:22   作者:  
PHP寫XML方法很多,這里主要介紹一下DOMDocument的用法,跟 JS大體上相同,其實(shí)非常簡單。
共分四個(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é)點(diǎn)列表getElementsByTagName然后foreach遍歷想要修改的節(jié)點(diǎn).

相關(guān)文章

  • PHP實(shí)現(xiàn)普通和定時(shí)跳轉(zhuǎn)的幾種方式

    PHP實(shí)現(xiàn)普通和定時(shí)跳轉(zhuǎn)的幾種方式

    這篇文章主要為大家詳細(xì)介紹了PHP實(shí)現(xiàn)普通和定時(shí)跳轉(zhuǎn)的幾種方式,文章通過代碼示例介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,需要的小伙伴可以參考下
    2023-11-11
  • 解析php dirname()與__FILE__常量的應(yīng)用

    解析php dirname()與__FILE__常量的應(yīng)用

    本篇文章是對php中的dirname()與__FILE__常量的應(yīng)用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06
  • 如何直接訪問php實(shí)例對象中的private屬性詳解

    如何直接訪問php實(shí)例對象中的private屬性詳解

    這篇文章主要給大家介紹了關(guān)于如何直接訪問php實(shí)例對象中private屬性的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-10-10
  • PHP中for與foreach的區(qū)別分析

    PHP中for與foreach的區(qū)別分析

    foreach 僅能用于數(shù)組,當(dāng)試圖將其用于其它數(shù)據(jù)類型或者一個(gè)未初始化的變量時(shí)會(huì)產(chǎn)生錯(cuò)誤,在PHP5中也能遍歷對象。
    2011-03-03
  • php入門教程之Zend Studio設(shè)置與開發(fā)實(shí)例

    php入門教程之Zend Studio設(shè)置與開發(fā)實(shí)例

    這篇文章主要介紹了php入門教程之Zend Studio設(shè)置與開發(fā),簡單分析了Zend Studio模板的設(shè)置與php簡單開發(fā)商品訂單程序的技巧,需要的朋友可以參考下
    2016-09-09
  • PHP偽造referer實(shí)例代碼

    PHP偽造referer實(shí)例代碼

    偽造referer實(shí)例代碼,主要用于一些突破防盜鏈
    2008-09-09
  • PHP入門教程之PHP操作MySQL的方法分析

    PHP入門教程之PHP操作MySQL的方法分析

    這篇文章主要介紹了PHP操作MySQL的方法,結(jié)合實(shí)例形式分析了php針對mysql數(shù)據(jù)庫的基本連接、查詢、修改、更新及刪除等常見技巧,并附帶了Zend開發(fā)環(huán)境的編碼設(shè)置方法說明,需要的朋友可以參考下
    2016-09-09
  • yii2超好用的日期組件和時(shí)間組件

    yii2超好用的日期組件和時(shí)間組件

    這篇文章主要介紹了yii2超好用的日期組件和時(shí)間組件,需要的朋友可以參考下
    2016-05-05
  • PHP編程實(shí)現(xiàn)腳本異步執(zhí)行的方法

    PHP編程實(shí)現(xiàn)腳本異步執(zhí)行的方法

    這篇文章主要介紹了PHP編程實(shí)現(xiàn)腳本異步執(zhí)行的方法,結(jié)合實(shí)例形式分析了php使用fsockopen()實(shí)現(xiàn)的腳本異步調(diào)用與執(zhí)行操作技巧,需要的朋友可以參考下
    2017-08-08
  • 兩種php實(shí)現(xiàn)圖片上傳的方法

    兩種php實(shí)現(xiàn)圖片上傳的方法

    這篇文章主要為大家介紹了兩種php實(shí)現(xiàn)圖片上傳的方法,利用form表單上傳,另一種方法是利用uploadify實(shí)現(xiàn)無刷新并且?guī)нM(jìn)度條的上傳,感興趣的小伙伴們可以參考一下
    2016-01-01

最新評(píng)論