php DOMDocument應用執行個體(XML建立、添加、刪除、修改)

來源:互聯網
上載者:User
  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 下一頁 尾頁

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.