PHP domelement Examples of manipulating XML documents

Source: Internet
Author: User
    1. /*

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

    9. <名字> Bones Jing
    10. <年龄> 140
    11. <介绍> Introduction Content
    12. */

Copy Code

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 (' student ');

  8. $itemElement = $itemsNodeList->item (0);//Get the first complete student information node
  9. $itemChildsNodeList = $itemElement->getelementsbytagname (' name ');//Get child node "name", perhaps with multiple names
  10. $itemChildNode = $itemChildsNodeList->item (0);//Get First name node
  11. echo $itemChildNode->nodevalue;//Output node value

  12. Encapsulation into functions

  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. Adding nodes

  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 (' student ');//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 performance ');
  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)? "Write: $bytes bytes": ' Save Failed ';

  40. Delete a node

  41. $xmldoc = new DOMDocument (' 1.0 ', ' UTF-8 ');
  42. $xmldoc->load (' datas.xml ');
  43. $student = $xmldoc->getelementsbytagname (' Student ')->item (2);//Locate the node you want to delete directly
  44. $student->parentnode->removechild ($student);//delete method of parent node
  45. $xmldoc->save (' datas.xml ');

  46. modifying node values

  47. $student = $xmldoc->getelementsbytagname (' Student ')->item (2);
  48. $student->getelementsbytagname (' Age ')->item (0)->nodevalue + = 10;
  49. $student->setattribute (' id ', ' 110 ');
  50. $xmldoc->save (' datas.xml ');

  51. Applying an Xpath lookup node

  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 Manipulation XML

  58. /*
  59. 1001
  60. 200 USD
  61. Daming
  62. Tian Long Eight Department
  63. 1002
  64. 321 USD
  65. Tom
  66. Laughing and proud of the lake
  67. 1004
  68. 182 USD
  69. John doe
  70. Readers
  71. */
  72. $xml = simplexml_load_file (' books.xml ');
  73. $books = $xml->book;
  74. Echo $books [1]->title. $books [1][' house '];//direct pointing to the second book
  75. foreach ($xml as $item) {
  76. echo $item->title, ", $item [' House '], '
    ';
  77. }
  78. ?>

Copy Code
  • 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.