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.