<?php
$xmlpatch = 'index.xml';
$_id = '';
$_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 = '';
$_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 = '';
$_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 = ;
//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 == ) {
   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 = '';
$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 增删改查的更多相关文章

  1. 使用python操作XML增删改查

    使用python操作XML增删改查 什么是XML? XML 指可扩展标记语言(EXtensible Markup Language) XML 是一种标记语言,很类似 HTML XML 的设计宗旨是传输 ...

  2. [原创]Linq to xml增删改查Linq 入门篇:分分钟带你遨游Linq to xml的世界

    本文原始作者博客 http://www.cnblogs.com/toutou Linq 入门篇(一):分分钟带你遨游linq to xml的世界 本文原创来自博客园 请叫我头头哥的博客, 请尊重版权, ...

  3. net对XML增删改查

    Pass:看公司代码,配置下拉框的功能,和下拉框的数字转文字.配置xml里面有下拉的value,name,这样界面直接显示数字,然后转译成中文 1.xml文件格式 <?xml version=& ...

  4. .net xml 增删改查基础复习及干货分享

    今天做做项目时,有一个需求需要用到一些固定的文本数据,觉得将这些需要存储的信息直接写在代码里很不友好,放在数据库中存储又觉得不够方便,自然就想到了使用xml来进行操作,我平常在项目中其实用到xml的机 ...

  5. c# XML增删改查

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  6. Linq to XML 增删改查

    Linq to XML同样是对原C#访问XML文件的方法的封装,简化了用xpath进行xml的查询以及增加,修改,删除xml元素的操作.C#访问XML文件的常用类:XmlDocument,XmlEle ...

  7. c#操作xml增删改查

    1.首先新建一个xml文件(Root是我写上的) 2. 3.直接上代码,更直观 (1)初始化xml /// <summary> /// 初始化xml /// </summary> ...

  8. Linq To Xml操作XML增删改查

    对XML文件的操作在平时项目中经常要运用到,比如用于存放一些配置相关的内容:本文将简单运用Linq TO Xml对XML进行操作,主要讲解对XML的创建.加载.增加.查询.修改以及删除:重点在于类XD ...

  9. 4.Linq To Xml操作XML增删改查

    转自https://www.cnblogs.com/wujy/p/3366812.html 对XML文件的操作在平时项目中经常要运用到,比如用于存放一些配置相关的内容:本文将简单运用Linq TO X ...

随机推荐

  1. C#: 异步委托

    http://www.cnblogs.com/yingzhongwen/p/4568350.html 讲了委托与事件,但是对异步委托研究得还不够深入. http://www.cnblogs.com/l ...

  2. 什么是XML

    什么是 XML? XML 指可扩展标记语言(EXtensible Markup Language) XML 是一种标记语言,很类似 HTML XML 的设计宗旨是传输数据,而非显示数据 XML 标签没 ...

  3. dataset 使用

    下面有例子说明: 首先我们需要打开一个联结: string MyConnString = "Driver={Microsoft Access Driver (*.mdb)}; DBQ=c:/ ...

  4. mydetails-yii1

    1.yii验证码多余的get a new code ,即使在main.php中配置了中文也是出现获取新图片,影响效果 需要把 <?php $this->widget('CCaptcha') ...

  5. SSAS计算列如果是中文名称时,必须要在名字外加中括号

    在SSAS中建计算列的时候,如果你给计算列起的是中文名字,一定记住要在名字外加中括号,比如下面这个例子中我们建了一个叫 客服流失数 的计算列 下面图中没有在计算列名称上加中括号这是错误的,因为使用中文 ...

  6. java小程序整理及排序算法

    1. 利用循环打印如下图形 ***** **** *** ** * public class Main { public static void main(String[] args) { // TO ...

  7. 160905、c3p0详细配置

    官方文档 : http://www.mchange.com/projects/c3p0/index.html <c3p0-config> <default-config> &l ...

  8. TI CC2541的狗日的Key

    被突如其来的一个bug困扰了好几天, 起因是, 按键接的红外接收器, 结果发现, 一旦按下之后, IEN1, P0IE的标识位bit5, 被不知道特么的谁归0了, 也就是说, 按键只能被按下一次, 再 ...

  9. http 报文 - 转

    1. http 报文 2. HTTP报文

  10. JVM 指令集

    指令码 助记符 说明 0x00 nop 什么都不做 0x01 aconst_null 将null推送至栈顶 0x02 iconst_m1 将int型-1推送至栈顶 0x03 iconst_0 将int ...