import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import org.junit.Test;
public class XPathReadXml {
private String xmlfile1 = "WebRoot/product2.xml";
private String xmlfile2 = "WebRoot/users.xml";
public Document getDocument(String xmlfile) throws DocumentException{
SAXReader reader = new SAXReader();
return reader.read(xmlfile);
}
//读取<product>下<price>的Text值
@Test
public void readText() throws DocumentException{
Document document = getDocument(xmlfile1);
//如果路径以斜线 / 开始, 那么该路径就表示到一个元素的绝对路径
String xmlPath1 = "/catalog/product/price";
//如果路径以双斜线 // 开头, 则表示选择文档中所有满足双斜线//之后规则的元素(无论层级关系)
String xmlPath2 = "//price";
List<Element> priceList = document.selectNodes(xmlPath1);
for (Element price : priceList) {
System.out.println("<"+price.getName()+">"+price.getText()+"</"+price.getName()+">");
}
System.out.println("-------------------");
Element product1 = document.getRootElement().element("product");
//product1的效果和document查询的效果是一样的
priceList = product1.selectNodes(xmlPath1);
for (Element price : priceList) {
System.out.println("<"+price.getName()+">"+price.getText()+"</"+price.getName()+">");
}
System.out.println("-------------------");
Element price1 = (Element) document.selectSingleNode(xmlPath1);
if(price1!=null)
System.out.println("<"+price1.getName()+">"+price1.getText()+"</"+price1.getName()+">");
System.out.println("-------------------");
price1 = (Element) document.selectSingleNode(xmlPath2);
System.out.println("<"+price1.getName()+">"+price1.getText()+"</"+price1.getName()+">");
System.out.println("-------------------");
priceList = document.selectNodes(xmlPath2);
for (Element price : priceList) {
System.out.println("<"+price.getName()+">"+price.getText()+"</"+price.getName()+">");
}
/**
* 输出结果:
<price>80.0</price>
<price>100.0</price>
-------------------
<price>80.0</price>
<price>100.0</price>
-------------------
<price>80.0</price>
-------------------
<price>80.0</price>
-------------------
<price>80.0</price>
<price>100.0</price>
**/
}
//验证用户登陆是否成功
@Test
public void valideLogin() throws DocumentException{
Document document = getDocument(xmlfile2);
String username = "aaa";
String password = "aaapass";
String xmlpath = "//user[@name='"+username+"' and @password='"+password+"']";
Node node = document.selectSingleNode(xmlpath);
if (node!=null) {
System.out.println("登陆成功!");
}else {
System.out.println("用户名和密码不存在!");
}
}
}
product2.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<catalog id="cata1">
<product category="HandTool" inventory="InStock">
<specifications weight="2.0kg">扳手</specifications>
<price street="香港街">80.0</price>
<notes>这是扳手</notes>
</product>
<product category="Table">
<specifications>桌椅</specifications>
<price street="澳门街" wholesale="部分">100.0</price>
</product>
</catalog>
users.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<users>
<user name="aaa" password="aaapass"></user>
<user name="bbb" password="bbbpass"></user>
<user name="ccc" password="cccpass"></user>
<user name="ddd" password="dddpass"></user>
</users>
关于XPath定位的用法情参见:XPathTutorial.chm(下载地址:http://download.csdn.net/detail/xh16319/4587406)