An in-depth explanation of PHP Domxpath's role in parsing XML files _php Tutorials

Source: Internet
Author: User
Tags tagname
When using

Examples of how PHP Domxpath are used:

    1. < ? PHP
    2. Header ("content-type:text/html;
      Charset=utf-8 ");
    3. $ URL = "http://www.google.com/ig/
      Api?weather=shenzhen ";
    4. Loading 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 ' temp: ', $temp _c, ' < br />';
    13. function Get_google_xml_data
      ($element, $tagname) {
    14. $ Tags = $element- >
      getElementsByTagName ($tagname);
    15. Get all the $tagname
    16. if ($items->length > 1) {
    17. return $items;
    18. }
    19. $ Tag = $tags- > item (0);
    20. Gets the first label named $tagname
    21. if ($tag->hasattributes ()) {
    22. Get the Data property
    23. $ attribute = $tag- >
      getattribute ("data");
    24. return $attribute;
    25. }else {
    26. return false;
    27. }
    28. }
    29. ?>
I hope that through the above code example, we can fully grasp the specific use of PHP Domxpath method.

http://www.bkjia.com/PHPjc/446187.html www.bkjia.com true http://www.bkjia.com/PHPjc/446187.html techarticle examples of how to use PHP Domxpath:? PHP header (content-type:text/html; Charset = Utf-8); $ url = http://www.google.com/ig/api?weather=shenzhen; Load XML content $ dom ...

  • Related Article

    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.