PHP DOMDocument Application Example (XML creation, addition, deletion, modification)

Source: Internet
Author: User
Tags save file
  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 ');//New node
  11. $index = $doc-createelement_x (' index ');//New node
  12. $url = $doc createattribute (' url ');//new property
  13. $patch = $doc createTextNode ($_htmlpatch);//New text value
  14. $url-AppendChild ($patch);//Set $patch text to the value of the.
  15. $id = $doc-CreateAttribute (' id ');
  16. $newsid = createTextNode ($_id), $doc;
  17. $id-AppendChild ($NEWSID);
  18. $title = $doc createattribute (' title ');
  19. $newstitle = createTextNode ($_title), $doc;
  20. $title-AppendChild ($newstitle);
  21. $content = $doc createTextNode ($_content);//Node value
  22. $author = $doc CreateAttribute (' author ');
  23. $newsauthor = createTextNode ($_author), $doc;
  24. $author-AppendChild ($newsauthor);
  25. $sendtime = CreateAttribute (' time '), $doc
  26. $newssendtime = createTextNode ($_sendtime), $doc;
  27. $sendtime-AppendChild ($newssendtime);
  28. $index-AppendChild ($id);//sets $id as the property of the index node, the following similar
  29. $index-AppendChild ($title);
  30. $index-AppendChild ($content);
  31. $index-AppendChild ($url);
  32. $index-AppendChild ($author);
  33. $index-AppendChild ($sendtime);
  34. $root-AppendChild ($index);//Set Index to root byte point
  35. $doc-AppendChild ($root);//set Root to follow node
  36. Save ($xmlpatch) $doc;//Save File
  37. Echo $xmlpatch. ' has create success ';
  38. ?>
  39. XML operations
Copy Code

  1. 2, add.php add function (similar to the index.php file, the main is to add a load with the $root = $doc, documentelement get with the node

  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;//Get root node (root)
  13. $index = createelement_x (' index '), $doc
  14. $url = $doc-createattribute (' url ');
  15. $patch = createTextNode ($_htmlpatch), $doc;
  16. $url-AppendChild ($patch);
  17. $id = $doc-CreateAttribute (' id ');
  18. $newsid = createTextNode ($_id), $doc;
  19. $id-AppendChild ($NEWSID);
  20. $title = $doc createattribute (' title ');
  21. $newstitle = createTextNode ($_title), $doc;
  22. $title-AppendChild ($newstitle);
  23. $content = createTextNode ($_content), $doc;
  24. $author = $doc CreateAttribute (' author ');
  25. $newsauthor = createTextNode ($_author), $doc;
  26. $author-AppendChild ($newsauthor);
  27. $sendtime = CreateAttribute (' time '), $doc
  28. $newssendtime = createTextNode ($_sendtime), $doc;
  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. ' have been added in '. $xmlpatch;
  39. } else {
  40. Echo ' XML file loaded error! ';
  41. }
  42. ?>
  43. XML Operations-Add
Copy Code

3edit.php Modify function (only the Title property value and the node value are modified) 1 2 Next last

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.