php中DOMDocument简单用法(XML创建、添加、删除、修改)

PHP写XML方法很多,这里主要介绍一下DOMDocument的用法,跟 JS大体上相同,其实非常简单。

共分四个文件,分别是创建、增加、删除、修改四个功能,变量都是写死的,改一改用$_POST方式接收就可以用了

创建XML:

 1 <?php
 2 $xmlpatch = 'index.xml';
 3 $_id = '1';
 4 $_title = 'title1';
 5 $_content = 'content1';
 6 $_author = 'author1';
 7 $_sendtime = 'time1';
 8 $_htmlpatch = '1.html';
 9  
10 $doc = new DOMDocument('1.0', 'utf-8');
11 $doc -> formatOutput = true;
12  
13 $root = $doc -> createElement('root');//新建节点
14  
15 $index = $doc -> createElement('index');//新建节点
16  
17 $url = $doc -> createAttribute('url');//新建属性
18 $patch = $doc -> createTextNode($_htmlpatch);//新建TEXT值
19 $url -> appendChild($patch);//将$patch文本设为$url属性的值
20  
21 $id = $doc -> createAttribute('id');
22 $newsid = $doc -> createTextNode($_id);
23 $id -> appendChild($newsid);
24  
25 $title = $doc -> createAttribute('title');
26 $newstitle = $doc -> createTextNode($_title);
27 $title -> appendChild($newstitle);
28  
29 $content = $doc -> createTextNode($_content);//节点值
30  
31 $author = $doc -> createAttribute('author');
32 $newsauthor = $doc -> createTextNode($_author);
33 $author -> appendChild($newsauthor);
34  
35 $sendtime = $doc -> createAttribute('time');
36 $newssendtime = $doc -> createTextNode($_sendtime);
37 $sendtime -> appendChild($newssendtime);
38  
39 $index -> appendChild($id);//将$id设为index节点的属性,以下类同
40 $index -> appendChild($title);
41 $index -> appendChild($content);
42 $index -> appendChild($url);
43 $index -> appendChild($author);
44 $index -> appendChild($sendtime);
45  
46 $root -> appendChild($index);//设置index为root字节点
47  
48 $doc -> appendChild($root);//设置root为跟节点
49  
50 $doc -> save($xmlpatch);//保存文件
51  
52 echo $xmlpatch . ' has create success';
53  
54 ?>

增加XMLadd.php 增加功能(跟index.php文件差不多,主要就是加个load载入跟 $root = $doc -> do//cumentElement获得跟节点):

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

修改XML:

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

XML删除功能:

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

 

文章转至:http://my.oschina.net/zhangb081511/blog/160113

 
posted @ 2015-10-28 11:00  岁月必然的流逝  阅读(428)  评论(0编辑  收藏  举报