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:
- <? PHP
- Header ("Content-type: text/html;
Charset = utf-8 ");
- $ Url = "http://www.google.com/ig/
Api? Weather = shenzhen ";
- // Load XML content
- $ Dom = new DOMDocument ();
- $ Dom->Load ($ url );
- $ Xpath = new DOMXPath ($ dom );
- $ Element = $ xpath->Query ("/xml_api_reply
/Weather/current_conditions ")->Item (0 );
- $ Condition = get_google_xml_data
($ Element, "condition ");
- $ Temp_c = get_google_xml_data
($ Element, "temp_c ");
- Echo 'weather: ', $ condition ,'< Br />';
- Echo 'temperature: ', $ temp_c ,'< Br />';
- Function get_google_xml_data
($ Element, $ tagname ){
- $ Tags = $ element->
GetElementsByTagName ($ tagname );
- // Get all $ tagnames
- If ($ items->Length>1 ){
- Return $ items;
- }
- $ Tag = $ tags->Item (0 );
- // Obtain the first tag named after $ tagname
- If ($ tag->HasAttributes ()){
- // Obtain data attributes
- $ Attribute = $ tag->
GetAttribute ("data ");
- Return $ attribute;
- } Else {
- Return false;
- }
- }
- ?>
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...