郑炎青吧 关注:6贴子:80
  • 0回复贴,共1
xpath是用来查找xml文档的一门语言,可以直接用xml对象调用xpath方法


一、 xpath(xml路径查询语言)
1. 精确查询
//person[1] 绝对路径第一个person节点
$test = $xml -> xpath('//person[1]');


/persons/person[1]' 相对路径第一个person节点
$test = $xml -> xpath('/persons/person[1]');


/persons/person[last()] 最后一个person节点
$test = $xml -> xpath('/persons/person[last()]');


/person[@id="2"] 属性id=2的person节点
$test = $xml -> xpath('//person[@id="2"]');


//person[age>20] person下的子节点age值大于20
$test = $xml -> xpath('//person[age>20]');




2. 模糊查询
//name[contains(text(),"xiao")] 查找含有xiao内容的name标签
$test = $xml -> xpath('//name[contains(text(),"xiao")]');


//title[contains(@attrName,’php’)]//查找属性名为attrName,且属性值包含的php的title节点的
$test = $xml -> xpath('//title[contains(@attrName,"php")]');



3. 等值查询//title[text()=’php’]//查找节点内容为php的title节点
$test = $xml -> xpath('//title[text()="php"]');




4. 之前的案例
所有的百度车联网Api的接口汇总网址:http://lbsyun.baidu.com/index.php?title=car

天气接口地址:http://api.map.baidu.com/telematics/v3/weather?location=%E5%B9%BF%E5%B7%9E&output=json&ak=DE93a33f736d1e75409e43a9501fcf80

参数说明:
location:城市名称, 建议使用urlencode()函数进行编码处理,防止用户输入特殊字符如&对url地址产生歧义:
output:返回的数据格式,常见的有xml和json两种。
ak:申请的ak密钥, 参考以下课件资料自行申请



//表示相对路径 /表示绝对路径 @声明接下来是属性 text()声明是文本 [expr]里面是表达式


回复
1楼2017-11-03 00:06