2008年10月24日 星期五

使用 XPath 對 XML文件做模糊查詢

XML 第一種儲存方式,使用 Node 來儲存資料
<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第二種儲存方式,使用 Attribute 來儲存資料

<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')]";