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;
}
)
}