Example of phpDOMElement for operating xml documents

Source: Internet
Author: User
Example of phpDOMElement for operating xml documents

  1. /*

  2. <班级>
  3. <学生 number="101">
  4. <名字> Sun Wukong
  5. <名字> Sun Walker
  6. <年龄> 123
  7. <介绍>&*$%特殊字串^&#$&
  8. <学生 number="10"2">

  9. <名字> Bai Gujing
  10. <年龄> 140
  11. <介绍> Introduction
  12. */

2. php code

  1. /**

  2. * DOMElement XML
  3. * Http://bbs.it-home.org
  4. */
  5. $ Xmldoc = new DOMDocument ('1. 0', 'utf-8 ');
  6. $ Xmldoc-> load ('datas. XML ');

  7. $ ItemsNodeList = $ xmldoc-> getElementsbyTagName ('studen ');

  8. $ ItemElement = $ itemsNodeList-> item (0); // obtain the first complete student information node.
  9. $ ItemChildsNodeList = $ itemElement-> getElementsbyTagName ('name'); // Obtain the subnode "name", which may have multiple names
  10. $ ItemChildNode = $ itemChildsNodeList-> item (0); // obtain the first name node.
  11. Echo $ itemChildNode-> nodeValue; // output node value

  12. // Encapsulate it into a function

  13. $ NodeArr = array ('name', 'age', 'Introduction ');
  14. Function getNodeVal ($ xmldoc, $ itemsName, $ nodeArr ){
  15. $ Items = $ xmldoc-> getElementsByTagName ($ itemsName );
  16. For ($ I = 0; $ I <$ items-> length; $ I ++ ){
  17. $ Item = $ items-> item ($ I );
  18. Foreach ($ nodeArr as $ node ){
  19. $ Data [$ I] [] = $ item-> getElementsByTagName ($ node)-> item (0)-> nodeValue;
  20. }
  21. }
  22. Return $ data;
  23. }

  24. $ Data = getNodeVal ($ xmldoc, 'Student ', $ nodeArr );

  25. Print_r ($ data );

  26. // Add a node

  27. $ Xmldoc = new DOMDocument ('1. 0', 'utf-8 ');
  28. $ Xmldoc-> load ('datas. XML ');
  29. $ Items = $ xmldoc-> getElementsByTagName ('class')-> item (0); // root node
  30. $ Student = $ xmldoc-> createElement ('studen'); // create a new student node
  31. $ Stu_name = $ xmldoc-> createElement ('name', 'Zhang San ');
  32. $ Stu_age = $ xmldoc-> createElement ('age', '15 ');
  33. $ Stu_intro = $ xmldoc-> createElement ('Introduction', 'strong hands-on ability and stable scores ');
  34. $ Items-> appendChild ($ student );
  35. $ Student-> appendChild ($ stu_name );
  36. $ Student-> appendChild ($ stu_age );
  37. $ Student-> appendChild ($ stu_intro );
  38. $ Bytes = $ xmldoc-> save ('datas. XML ');
  39. Echo ($ bytes )? "Written: $ bytes": 'failed to save ';

  40. // Delete a node

  41. $ Xmldoc = new DOMDocument ('1. 0', 'utf-8 ');
  42. $ Xmldoc-> load ('datas. XML ');
  43. $ Student = $ xmldoc-> getElementsByTagName ('studen')-> item (2); // locate the node to be deleted.
  44. $ Student-> parentNode-> removeChild ($ student); // delete a parent node
  45. $ Xmldoc-> save ('datas. XML ');

  46. // Modify the node value

  47. $ Student = $ xmldoc-> getElementsByTagName ('studen')-> item (2 );
  48. $ Student-> getElementsByTagName ('age')-> item (0)-> nodeValue + = 10;
  49. $ Student-> setAttribute ('id', '123 ');
  50. $ Xmldoc-> save ('datas. XML ');

  51. // Apply Xpath to find nodes

  52. $ Xml = new DOMDocument ('1. 0', 'utf-8 ');

  53. $ Xml-> load ('Dat. XML ');
  54. $ Xpath = new DOMXPath ($ xml );
  55. $ NodeList = $ xpath-> query ('/aaa/bbb/ddd/fff ');
  56. Echo $ nodeList-> item (0)-> nodeValue;

  57. // SimpleXML class operation xml

  58. /*
  59. 1001
  60. $200
  61. Daming
  62. Tianlong Babu
  63. 1002
  64. $321
  65. Zhang San
  66. Swordsman
  67. 1004
  68. $182
  69. Li Si
  70. Reader
  71. */
  72. $ Xml = simplexml_load_file ('books. XML ');
  73. $ Books = $ xml-> book;
  74. Echo $ books [1]-> title. $ books [1] ['House']; // direct to the second book
  75. Foreach ($ xml as $ item ){
  76. Echo $ item-> title, '', $ item ['House'],'
    ';
  77. }
  78. ?>

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.