php中DOMDocument簡單用法(XML建立、添加、刪除、修改)

來源:互聯網
上載者:User

PHP寫XML方法很多,這裡主要介紹一下DOMDocument的用法,跟 JS大體上相同,其實非常簡單。

共分四個檔案,分別是建立、增加、刪除、修改四個功能,變數都是寫死的,改一改用$_POST方式接收就可以用了

//index.php 建立功能

<?php
$xmlpatch = 'index.xml';
$_id = '1';
$_title = 'title1';
$_content = 'content1';
$_author = 'author1';
$_sendtime = 'time1';
$_htmlpatch = '1.html';

$doc = new DOMDocument('1.0', 'utf-8');
$doc -> formatOutput = true;

$root = $doc -> createElement('root');//建立節點

$index = $doc -> createElement('index');//建立節點

$url = $doc -> createAttribute('url');//建立屬性
$patch = $doc -> createTextNode($_htmlpatch);//建立TEXT值
$url -> appendChild($patch);//將$patch文本設為$url屬性的值

$id = $doc -> createAttribute('id');
$newsid = $doc -> createTextNode($_id);
$id -> appendChild($newsid);

$title = $doc -> createAttribute('title');
$newstitle = $doc -> createTextNode($_title);
$title -> appendChild($newstitle);

$content = $doc -> createTextNode($_content);//節點值

$author = $doc -> createAttribute('author');
$newsauthor = $doc -> createTextNode($_author);
$author -> appendChild($newsauthor);

$sendtime = $doc -> createAttribute('time');
$newssendtime = $doc -> createTextNode($_sendtime);
$sendtime -> appendChild($newssendtime);

$index -> appendChild($id);//將$id設為index節點的屬性,以下類同
$index -> appendChild($title);
$index -> appendChild($content);
$index -> appendChild($url);
$index -> appendChild($author);
$index -> appendChild($sendtime);

$root -> appendChild($index);//設定index為root位元組點

$doc -> appendChild($root);//設定root為跟節點

$doc -> save($xmlpatch);//儲存檔案

echo $xmlpatch . ' has create success';

?>

<!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>

<body>
</body>
</html>

//add.php 增加功能(跟index.php檔案差不多,主要就是加個load載入跟 $root = $doc -> documentElement獲得跟節點

<?php
$xmlpatch = 'index.xml';
$_id = '2';
$_title = 'title2';
$_content = 'content2';
$_author = 'author2';
$_sendtime = 'time2';
$_htmlpatch = '2.html';

$doc = new DOMDocument();
$doc -> formatOutput = true;
if($doc -> load($xmlpatch)) {
$root = $doc -> documentElement;//獲得根節點(root)
$index = $doc -> createElement('index');

$url = $doc -> createAttribute('url');
$patch = $doc -> createTextNode($_htmlpatch);
$url -> appendChild($patch);

$id = $doc -> createAttribute('id');
$newsid = $doc -> createTextNode($_id);
$id -> appendChild($newsid);

$title = $doc -> createAttribute('title');
$newstitle = $doc -> createTextNode($_title);
$title -> appendChild($newstitle);

$content = $doc -> createTextNode($_content);

$author = $doc -> createAttribute('author');
$newsauthor = $doc -> createTextNode($_author);
$author -> appendChild($newsauthor);

$sendtime = $doc -> createAttribute('time');
$newssendtime = $doc -> createTextNode($_sendtime);
$sendtime -> appendChild($newssendtime);

$index -> appendChild($id);
$index -> appendChild($title);
$index -> appendChild($content);
$index -> appendChild($url);
$index -> appendChild($author);
$index -> appendChild($sendtime);

$root -> appendChild($index);

$doc -> save($xmlpatch);

echo $_id . ' has been added in ' . $xmlpatch;

} 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>

<body>
</body>
</html>

//edit.php 修改功能(這裡只修改title屬性值 跟節點值)

<?php
$xmlpatch = 'index.xml';
$_id = '2';
$_title = 'has been changed';
$_content = 'has been changed';

$doc = new DOMDocument();
$doc -> formatOutput = true;

if($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!';
}

?>
<!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>

<body>
</body>
</html>

//del.php 刪除功能

<?php
$xmlpatch = 'index.xml';
$_id = '2';

$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!';
}

?>
<!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>

<body>
</body>
</html>

 

總結一下,建立跟添加主要用的就是create跟appendChild,create後邊跟Element就是建立節點,跟Attribute就是建立屬性,TextNode就是建立值,然後appendChild就是設定從屬關係,這麼一看非常簡單。

刪除與修改都是用先獲得節點列表getElementsByTagName然後foreach遍曆想要修改的節點

來自: http://hi.baidu.com/chickenlove/blog/item/e960b34f4450253bafc3abd7.html

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.