码迷,mamicode.com
首页 > 其他好文 > 详细

XML 增删改查

时间:2014-11-01 20:29:50      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   for   sp   文件   div   on   log   

 
<?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_x(root);//新建节点
$index = $doc -> createElement_x(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;


//增加
$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_x(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!;
}



//edit.php 修改功能(这里只修改title属性值 跟节点值)
$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_r(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!;
}


//del.php 删除功能

$xmlpatch = index.xml;
$_id = 2;
$doc = new DOMDocument();
$doc -> formatOutput = true;
if($doc -> load($xmlpatch)) {
    $root = $doc -> documentElement;
    $elm = $root -> getElementsByTagName_r(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!;
}


?>

 

XML 增删改查

标签:style   blog   color   for   sp   文件   div   on   log   

原文地址:http://www.cnblogs.com/jthb/p/4067666.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!