当前位置:Gxlcms > PHP教程 > phpDOMDocument应用实例(XML创建、添加、删除、修改)

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

时间:2021-07-01 10:21:17 帮助过:26人阅读

  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 下一页 尾页

人气教程排行