php DOMDocument应用实例(XML创建、添加、删除、修改)

php中文网
发布: 2016-07-25 09:04:08
原创
1323人浏览过
  1. $xmlpatch = 'index.xml';
  2. $_id = '1';
  3. $_title = 'title1';
  4. $_content = 'content1';
  5. $_author = 'author1';
  6. $_sendtime = 'time1';
  7. $_htmlpatch = '1.html';
  8. $doc = new DOMDocument('1.0', 'utf-8');
  9. $doc -> formatOutput = true;
  10. $root = $doc -> createElement_x('root');//新建节点
  11. $index = $doc -> createElement_x('index');//新建节点
  12. $url = $doc -> createAttribute('url');//新建属性
  13. $patch = $doc -> createTextNode($_htmlpatch);//新建TEXT值
  14. $url -> appendChild($patch);//将$patch文本设为$url属性的值
  15. $id = $doc -> createAttribute('id');
  16. $newsid = $doc -> createTextNode($_id);
  17. $id -> appendChild($newsid);
  18. $title = $doc -> createAttribute('title');
  19. $newstitle = $doc -> createTextNode($_title);
  20. $title -> appendChild($newstitle);
  21. $content = $doc -> createTextNode($_content);//节点值
  22. $author = $doc -> createAttribute('author');
  23. $newsauthor = $doc -> createTextNode($_author);
  24. $author -> appendChild($newsauthor);
  25. $sendtime = $doc -> createAttribute('time');
  26. $newssendtime = $doc -> createTextNode($_sendtime);
  27. $sendtime -> appendChild($newssendtime);
  28. $index -> appendChild($id);//将$id设为index节点的属性,以下类同
  29. $index -> appendChild($title);
  30. $index -> appendChild($content);
  31. $index -> appendChild($url);
  32. $index -> appendChild($author);
  33. $index -> appendChild($sendtime);
  34. $root -> appendChild($index);//设置index为root字节点
  35. $doc -> appendChild($root);//设置root为跟节点
  36. $doc -> save($xmlpatch);//保存文件
  37. echo $xmlpatch . ' has create success';
  38. ?>
  39. XML操作
复制代码

  1. 2、add.php 增加功能(跟index.php文件差不多,主要就是加个load载入跟 $root = $doc -> documentElement获得跟节点

  2. $xmlpatch = 'index.xml';
  3. $_id = '2';
  4. $_title = 'title2';
  5. $_content = 'content2';
  6. $_author = 'author2';
  7. $_sendtime = 'time2';
  8. $_htmlpatch = '2.html';
  9. $doc = new DOMDocument();
  10. $doc -> formatOutput = true;
  11. if($doc -> load($xmlpatch)) {
  12. $root = $doc -> documentElement;//获得根节点(root)
  13. $index = $doc -> createElement_x('index');
  14. $url = $doc -> createAttribute('url');
  15. $patch = $doc -> createTextNode($_htmlpatch);
  16. $url -> appendChild($patch);
  17. $id = $doc -> createAttribute('id');
  18. $newsid = $doc -> createTextNode($_id);
  19. $id -> appendChild($newsid);
  20. $title = $doc -> createAttribute('title');
  21. $newstitle = $doc -> createTextNode($_title);
  22. $title -> appendChild($newstitle);
  23. $content = $doc -> createTextNode($_content);
  24. $author = $doc -> createAttribute('author');
  25. $newsauthor = $doc -> createTextNode($_author);
  26. $author -> appendChild($newsauthor);
  27. $sendtime = $doc -> createAttribute('time');
  28. $newssendtime = $doc -> createTextNode($_sendtime);
  29. $sendtime -> appendChild($newssendtime);
  30. $index -> appendChild($id);
  31. $index -> appendChild($title);
  32. $index -> appendChild($content);
  33. $index -> appendChild($url);
  34. $index -> appendChild($author);
  35. $index -> appendChild($sendtime);
  36. $root -> appendChild($index);
  37. $doc -> save($xmlpatch);
  38. echo $_id . ' has been added in ' . $xmlpatch;
  39. } else {
  40. echo 'xml file loaded error!';
  41. }
  42. ?>
  43. XML操作-添加
复制代码

3edit.php 修改功能(只修改title属性值 跟节点值) 1 2 下一页 尾页



PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号