How to correctly create XML files using the PHP DOM-XML

Source: Internet
Author: User

When creating an XML file and parsing it, we usually use

Application code example for PHP DOM-XML:

  1. <? Php
  2. /**
  3. * Topic: Create and parse XML files using PHP DOM-XML
  4. * Source: http://www.php.net/domxml
  5. * Reference: http://www.zugeschaut-und-mitgebaut.de/php/extension.domxml.html
  6. * Author: urs@circle.ch, 16-1-2001
  7. *
  8. */
  9. // Use the PHP DOM-XML to create and parse XML files
  10. // Create an XML document object. Subsequent processing will be performed on this basis.
  11. $ Doc = new_xmldoc ("1.0 ");
  12. // Create a root node and set an attribute
  13. $ Root = $ doc->Add_root ("faq ");
  14. $ Root->Setattr ("page", "32 ");
  15. // Subnode
  16. $ One = $ root->New_child ("question ","");
  17. // Set attributes for the subnode
  18. $ One->Setattr ("number", "1 ");
  19. // Question also creates a subnode and assigns a value to it
  20. $ One->New_child ("text", "1. Where to get libxml-2.0.0? ");
  21. $ One->New_child ("answer", "You can download the latest
  22. Release of libxml either as a source archive or
  23. RPM package from http://www.xmlsoft.org.
  24. The current version is libxml2-2.2.1 .");
  25. $ Two = $ root->New_child ("question ","");
  26. $ Two->Setattr ("number", "2 ");
  27. $ Two->New_child ("text", "2. How to configure PHP4? ");
  28. // Create a node with no direct value assignment
  29. $ Twoone = $ two->New_child ("answer ","");
  30. // Assign a value to it separately
  31. $ Twoone->Set_content ("DIR is the libxml install directory
  32. (If you just use -- with-dom it ults
  33. To/usr), I needed to use -- with-dom =/usr/local ");
  34. $ Three = $ root->New_child ("question ","");
  35. $ Three->Setattr ("number", "7 ");
  36. $ Three->New_child ("text", "7. How to use dom xml function? ");
  37. $ Three->New_child ("answer", "Read this document source
  38. A simple example .");
  39. // Output to Browser
  40. Print ("< Pre>". Htmlspecialchars ($ doc->Dumpmem ())."</Pre>");
  41. // Write to file
  42. // Write back to file
  43. $ Fp = fopen ("test_dom.xml", "w + ");
  44. Fwrite ($ fp, $ doc->Dumpmem (), strlen ($ doc->Dumpmem ()));
  45. Fclose ($ fp );
  46. // Now use xpath to get content from the XML document
  47. $ Doc = xmldoc (join ("", file ("test_dom.xml ")));
  48. $ Ctx = xpath_new_context ($ doc );
  49. // All objects
  50. $ Foo = xpath_eval ($ ctx, "// child ::*");
  51. Print_r ($ foo );
  52. Print ("< Br/> < Br/>");
  53. // Text node object
  54. $ Foo = xpath_eval ($ ctx, "// text ");
  55. Print_r ($ foo );
  56. Print ("< Br/> < Br/>");
  57. // The first text node object
  58. $ Foo = xpath_eval ($ ctx, "// text [1]");
  59. Print_r ($ foo );
  60. Print ("< Br/> < Br/>");
  61. // The second text node object
  62. $ Foo = xpath_eval ($ ctx, "// text [2]");
  63. Print_r ($ foo );
  64. Print ("< Br/> < Br/>");
  65. // The third answer object
  66. $ Foo = xpath_eval ($ ctx, "// answer [3]");
  67. Print_r ($ foo );
  68. Print ("< Br/> < Br/>");
  69. // Type, name, and content of the third text node
  70. $ Foo = xpath_eval ($ ctx, "// text [3]");
  71. $ Tmp = $ foo->Nodeset;
  72. Print_r ($ tmp );
  73. Print ("< Br/>");
  74. Print ($ tmp [0]->Type ).";";
  75. Print ($ tmp [0]->Name ).";";
  76. Print ($ tmp [0]->Content );
  77. ?> 

Note that the PHP DOM-XML can only run on PHPPHP4.0.x + linux


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.