Home  >  Article  >  Backend Development  >  In-depth interpretation of the role of PHP DOMXPath in XML file parsing_PHP Tutorial

In-depth interpretation of the role of PHP DOMXPath in XML file parsing_PHP Tutorial

WBOY
WBOYOriginal
2016-07-15 13:31:451013browse

Using

Example of how to use PHP DOMXPath:

    1. < ?PHP  
    2. header("Content-type:text/html; 
      Charset=utf-8");  
    3. $url = "http://www.google.com/ig/
      api?weather=shenzhen"
      ;  
    4. // 加载XML内容  
    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 '天气:', $condition, '< br />';  
    12. echo '温度:', $temp_c, '< br />';  
    13. function get_google_xml_data
      ($element, $tagname) {  
    14. $tags = $element->
      getElementsByTagName($tagname);   
    15. // 取得所有的$tagname  
    16. if ($items->length > 1) {  
    17. return $items;  
    18. }  
    19. $tag = $tags->item(0);   
    20. // 获取第一个以$tagname命名的标签  
    21. if ($tag->hasAttributes()) {   
    22. // 获取data属性  
    23. $attribute = $tag->
      getAttribute("data");
    24. return $attribute;
    25. }else {
    26. return false;
    27. ?>
    28. Hope that through the above code example, Everyone can fully understand the specific use of PHP DOMXPath. http://www.bkjia.com/PHPjc/446187.html
    29. www.bkjia.com
    true
http: //www.bkjia.com/PHPjc/446187.html
TechArticle

Example of using PHP DOMXPath: ?PHP header(Content-type:text/html; Charset = utf - 8); $ url = http://www.google.com/ig/ api?weather=shenzhen; //Load XML content $dom...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn