In-depth explanation of the role of PHPDOMXPath in parsing XML files _ PHP Tutorial

Source: Internet
Author: User
The role of PHPDOMXPath in parsing XML files is explained in depth. How to use PHPDOMXPath :? PHPheader (Content-type: texthtml; Charsetutf-8); $ urlwww. google. comigapi? Weathershenzhen; load XML Content $ dom in use

Example of using PHP DOMXPath:

    1. <? PHP
    2. Header ("Content-type: text/html;
      Charset = utf-8 ");
    3. $ Url = "http://www.google.com/ig/
      Api? Weather = shenzhen ";
    4. // Load XML content
    5. $ Dom = new DOMDocument ();
    6. $ Dom->Load ($ url );
    7. $ Xpath = new DOMXPath ($ dom );
    8. $ Element = $ xpath->Query ("/xml_api_reply
      /Weather/current_conditions ")->Item (0 );
    9. $ Condition = get_google_xml_data
      ($ Element, "condition ");
    10. $ Temp_c = get_google_xml_data
      ($ Element, "temp_c ");
    11. Echo 'weather: ', $ condition ,'< Br />';
    12. Echo 'temperature: ', $ temp_c ,'< Br />';
    13. Function get_google_xml_data
      ($ Element, $ tagname ){
    14. $ Tags = $ element->
      GetElementsByTagName ($ tagname );
    15. // Get all $ tagnames
    16. If ($ items->Length>1 ){
    17. Return $ items;
    18. }
    19. $ Tag = $ tags->Item (0 );
    20. // Obtain the first tag named after $ tagname
    21. If ($ tag->HasAttributes ()){
    22. // Obtain data attributes
    23. $ Attribute = $ tag->
      GetAttribute ("data ");
    24. Return $ attribute;
    25. } Else {
    26. Return false;
    27. }
    28. }
    29. ?>
We hope that through the code example above, you can fully understand the specific usage of PHP DOMXPath.

Example of using PHP DOMXPath in export :? PHP header (Content-type: text/html; Charset = utf-8); $ url = http://www.google.com/ig/ api? Weather = shenzhen; // load XML Content $ dom...

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.