Home  >  Article  >  Backend Development  >  php DOMDocument application example (XML creation, addition, deletion, modification)

php DOMDocument application example (XML creation, addition, deletion, modification)

WBOY
WBOYOriginal
2016-07-25 09:04:081129browse
  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 attribute
  13. $patch = $doc -> createTextNode($_htmlpatch);//New TEXT value
  14. $url -> appendChild($patch);//Set $patch text to $url attribute The value of
  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);//Node value
  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);//Set $id as the attribute of the index node, the following is similar to
  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 Click
  35. $doc -> appendChild($root);//Set root as the follow node
  36. $doc -> save($xmlpatch);//Save the file
  37. echo $xmlpatch . ' has create success';
  38. ?> ;
  39. < ;html xmlns="http://www.w3.org/1999/xhtml">
  40. XML operation
Copy code

  1. 2. add.php adds functions (similar to the index.php file, mainly adding a load and $root = $doc -> documentElement gets 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 the root node (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 operation -Add
Copy code

3edit.php modification function (only modify the title attribute value and node value) 1 2 Next page Last page



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn