xpath的用法

xpath 的定位

 

XML第一种存储方式 .xml

<users>

<user>

   <username>huo</username>

   <password>123</password>

   <createtime>2008-06-17</createtime>

</user>

</users>

xPath查询如:

等值查询:

String xPath = "users/user[username='huo' and password='123']";

模糊查询:

String xPath = "users/user[contains(username,'huo') and contains(password,'123')]";

 

XML第二种存储方式 .xml

<users>

<user username="huo" password="123" createtime="2008-06-17" />

</users>

xPath查询如:加"@" 用以查询属性值

等值查询:

String xPath = "users/user[@username='huo' and @password='123']";

模糊查询:

String xPath = "users/user[contains(@username,'huo') and contains(@password,'123')]";

 

 

 

js里面使用xpath

var newXml = '<root><rows><a>a</a><b>b</b></rows><rows><a>aa</a></rows></root>';

var docs=getXmlDoc(newXml);

docs.setProperty("SelectionLanguage", "XPath");

docs.selectNodes('/root/rows/b')[0].xml

docs.documentElement.selectNodes('/root/rows/b')[0].xml

 

function getXmlDoc(xml) {

  return Try.these(

    function(){

      var parser=new DOMParser();

      var xmlDoc=parser.parseFromString(xml,"text/xml");

      return xmlDoc;

    },

    function(){

      xmlDoc= new ActiveXObject("Microsoft.XMLDOM");

      xmlDoc.async=false;

      if (xmlDoc.loadXML(xml))

      return xmlDoc;

    }

  )

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值