php DOMDocument應用程式實例(XML建立、新增、刪除、修改)

WBOY
發布: 2016-07-25 09:04:08
原創
1127 人瀏覽過
  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 DOMDoDoment ('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 = $docdoc -> appendChild($newsauthor);
  26. $sendtime = $docdoc - > createAttribute('time');
  27. $newssendtime = $doc -> createTextNode($_sendtime);
  28. $sendtime -> appendChild($newssendtime);
  29. $index -> appendChild($id); //將$id設為index節點的屬性,以下類別同
  30. $index -> appendChild($title);
  31. $index -> appendChild($content);
  32. $index -> appendChild( $url);
  33. $index -> appendChild($author);
  34. $index -> appendChild($sendtime);
  35. $root -> appendChild($index);//設定index為root節點
  36. $doc -> appendChild($root);//設定root為跟節點
  37. $doc -> save($xmlpatch);//儲存檔案
  38. echo $xmlpatch . ' has create success ';
  39. ?>
  40. XML操作
複製程式碼

  1. 增加功能(跟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.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!