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)建功能
<?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)
<?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)值)
<?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 刪除功能
<?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).
//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).
您可能感興趣的文章:
- PHP使用DOMDocument類生成HTML實(shí)例(包含常見標(biāo)簽元素)
- 如何解決php domdocument找不到的問題
- PHP中使用DOMDocument來處理HTML、XML文檔的示例
- PHP讀取XML文件的方法實(shí)例總結(jié)【DOMDocument及simplexml方法】
- PHP創(chuàng)建XML的方法示例【基于DOMDocument類及SimpleXMLElement類】
- PHP基于DOMDocument解析和生成xml的方法分析
- PHP 中 DOMDocument保存xml時(shí)中文出現(xiàn)亂碼問題的解決方案
- PHP XML操作類DOMDocument
- php基于DOMDocument操作頁面元素實(shí)例
相關(guā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)用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06如何直接訪問php實(shí)例對象中的private屬性詳解
這篇文章主要給大家介紹了關(guān)于如何直接訪問php實(shí)例對象中private屬性的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-10-10php入門教程之Zend Studio設(shè)置與開發(fā)實(shí)例
這篇文章主要介紹了php入門教程之Zend Studio設(shè)置與開發(fā),簡單分析了Zend Studio模板的設(shè)置與php簡單開發(fā)商品訂單程序的技巧,需要的朋友可以參考下2016-09-09PHP編程實(shí)現(xiàn)腳本異步執(zhí)行的方法
這篇文章主要介紹了PHP編程實(shí)現(xiàn)腳本異步執(zhí)行的方法,結(jié)合實(shí)例形式分析了php使用fsockopen()實(shí)現(xiàn)的腳本異步調(diào)用與執(zhí)行操作技巧,需要的朋友可以參考下2017-08-08