Interpreting the role of PHPDOMDocument in parsing XML files _ PHP Tutorial

Source: Internet
Author: User
Interpret the role of PHPDOMDocument in parsing XML files. When using the XML file used, we also take the simplexml method for php xml processing as an example. the file name is me. xml. The code is as follows: how does PHPXML process XML code? Xmlversi in use

The XML file used is also named "simple Xml for Php XML processing". 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 Overview 1 </title>
  5. <Details> details 1 </details>
  6. </Post>
  7. <Post>
  8. <Title id = "2"> php xml processing Introduction 2 </title>
  9. <Details> details 2 </details>
  10. </Post>
  11. <Post>
  12. <Title id = "3"> php xml processing Introduction 3 </title>
  13. <Details> details 3 </details>
  14. </Post>
  15. </Phplamp>

Then, you need to use PHP DOMDocument to process the file and parse all the elements in the XML file. The code is as follows, which contains comments.

PHP DOMDocument parse XML file code

 
 
  1. <? Php
  2. // Create a DOMDocument object first
  3. $ Xml = new DOMDocument ();
  4. // Load the Xml file
  5. $ Xml-> load ("me. xml ");
  6. // Obtain all post tags
  7. $ PostDom = $ xml->
    GetElementsByTagName ("post ");
  8. // Cyclically traverse the post tag
  9. Foreach ($ postDom as $ post ){
  10. // Obtain the Title label Node
  11. $ Title = $ post->
    GetElementsByTagName ("title ");
  12. /**
  13. * The Id attribute of the Title tag to be obtained must be divided into two parts.
  14. * 1. obtain all attributes of the title
    The list is $ title-> item (0)-> attributes
  15. * 2. obtain the attributes of the id in the title,
    Because it is in the first place, item (0) is used)
  16. *
  17. * Tips:
  18. * If you take the attribute value, you can use item (*)-> nodeValue
  19. * If an attribute tag is used, you can use item (*)-> nodeName
  20. * If the attribute type is used, you can use 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/> ";
  25. }
  26. ?>

This is only one method. PHP DOMDocument is quite powerful and there are more parsing methods and strategies to be explored.


In addition, the XML file used by idea takes simplexml for Php Xml processing as an 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.