//利用Xpath找出第二本书的书名
@Test
public void test1() throws Exception{
SAXReader reader=
new SAXReader();
Document document=reader.read(
"src/Demo2.xml");
Element book_2=(Element)document.selectNodes(
"//书名").
get(
1);
String text_value=
book_2.getText();
System.out.println(text_value);
}
@Test
public void test() throws Exception{
//username和password 是通过用户提交得来的数据
String username=
"aaa";
String password=
"123";
//验证这个是否正确
SAXReader reader=
new SAXReader();
Document document=reader.read(
"src/user.xml");
Node node=document.selectSingleNode(
"//user[@username='"+username+
"' and @password='"+password+
"']");
if(node!=
null){
System.out.println(
"正确");
}
else System.
out.println(
"验证失败");
}
转载请注明原文地址: https://mac.8miu.com/read-508459.html