Using
Example of how to use PHP DOMXPath:
- < ?PHP
- header("Content-type:text/html;
Charset=utf-8"); - $url = "http://www.google.com/ig/
api?weather=shenzhen"; - // 加载XML内容
- $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 '天气:', $condition, '< br />';
-
echo '温度:', $temp_c, '< br />';
- function get_google_xml_data
($element, $tagname) { -
$tags = $element->
getElementsByTagName($tagname);
- // 取得所有的$tagname
-
if ($items->length > 1) {
- return $items;
- }
-
$tag = $tags->item(0);
- // 获取第一个以$tagname命名的标签
-
if ($tag->hasAttributes()) {
- // 获取data属性
-
$attribute = $tag->
getAttribute("data");
- return $attribute;
- }else {
- return false;
- ?>
-
-
Hope that through the above code example, Everyone can fully understand the specific use of PHP DOMXPath.
http://www.bkjia.com/PHPjc/446187.html
www.bkjia.com
truehttp: //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