设为首页 收藏本站
查看: 819|回复: 0

[经验分享] php中DOMDocument简单用法(XML创建、添加、删除、修改)

[复制链接]

尚未签到

发表于 2015-8-29 11:40:53 | 显示全部楼层 |阅读模式
  
  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

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-105888-1-1.html 上篇帖子: PHP基础语法 下篇帖子: php搜索文件
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表