Interpreting the role of PHP DOMDocument in parsing XML files _php tutorial

Source: Internet
Author: User
When using

The XML file used is also an example of XML in the article "How to use PHP XML to Process SimpleXML", the file name is: Me.xml. The code is as follows:

PHP XML processing XML code

  1. < ? xml version = "1.0" encoding="utf-8"?>
  2. < phplamp >
  3. < post >
  4. < title ID="1">PHP XML processing Introduction < /title >
  5. < details > detail one < /details>
  6. < /post >
  7. < post >
  8. < title ID="2">PHP XML processing Introduction two < /title >
  9. < details> content two < /details>
  10. < /post >
  11. < post >
  12. < title ID="3">PHP XML processing Introduction three < /title >
  13. < details > content three < /details>
  14. < /post >
  15. < /phplamp >

You then need to use PHP DOMDocument to process the file and parse any elements in the XML file. The code is as follows, with comments inside.

PHP DOMDocument parsing xml file code

 
 
  1. < ? PHP
  2. The first thing to do is to build a DOMDocument object
  3. $ XML = New DOMDocument ();
  4. Loading an XML file
  5. $xml- > load ("Me.xml");
  6. Get all the post labels
  7. $ Postdom = $xml- >
    getElementsByTagName ("post");
  8. Looping through post labels
  9. foreach ($postDom as $post) {
  10. Get the title Tag node
  11. $ title = $post- >
    getElementsByTagName ("title");
  12. /**
  13. * To get the id attribute of the title tag, go to section two.
  14. * 1. That gets all the attributes in the title.
    List is $title->Item (0)->attributes
  15. * 2. Gets the property of the ID in the title,
    Because it's in the first place, so with item (0)
  16. *
  17. * Tip:
  18. * If the value of the attribute can be taken with item (*)- > NodeValue
  19. * If the attribute tag can be used with item (*)- > NodeName
  20. * If the type of the attribute can be used with item (*)- > NodeType
  21. */
  22. echo "Id:". $title->Item (0)->
    attributes- > Item (0)- > NodeValue. "< br />";
  23. echo "Title:". $title->
    Item (0)- > NodeValue. "< br />";
  24. echo "Details:". $post->
    getElementsByTagName ("Details")- > I
    TEM (0)->nodevalue. "< br /> < BR />";
  25. }
  26. ?>

This is just a way, PHP DOMDocument is quite powerful, there are more analytic methods and strategies, waiting for you to explore.


http://www.bkjia.com/PHPjc/446191.html www.bkjia.com true http://www.bkjia.com/PHPjc/446191.html techarticle The XML file used in the use of XML is also used in PHP XML processing of the SimpleXML use of the text in the case of a simple example, the file name is: Me.xml. The code is as follows: PHP XML processing XML code? XML Versi ...

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