Problem: When using dom4j to query deep hierarchical structure nodes (labels, attributes, text), it is more troublesome! ! !
xpath is generated in this case-mainly used to quickly obtain the required [node object].
1) Import xPath supports jar package. jaxen-1.1-beta-6.jar
Node> selectNodes("xpath expression"); Query multiple node objects
Node selectSingleNode("xpath expression"); Query a node object
xPath syntax
# Starting from the root position of xml or a child element (a hierarchy)
Yes: Then it means that login is successful
没有: Then indicate the failure of login
## use as a database User.xml to store users The data code is as follows:import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
/**
* xpath案例: 模拟用户登录效果
* @author APPle
*
*/
public class Demo3 {
public static void main(String[] args)throws Exception{
//1.获取用户输入的用户名和密码
BufferedReader br =
new BufferedReader(new InputStreamReader(System.in));//封装键盘录入,输入流
System.out.println("请输入用户名:");
String name = br.readLine();
System.out.println("请输入密码:");
String password = br.readLine();
//2.到“数据库”中查询是否有对应的用户
//对应的用户: 在user.xml文件中找到一个
//name属性值为‘用户输入’,且password属性值为‘用户输入’的user标签
Document doc = new SAXReader().read(new File("./src/user.xml"));
Element userElem = (Element)doc.selectSingleNode("//user[@name='" +name +"' and @password='"+password+"']");
//在字符串中拼接变量的方法——先加一个双引号,再把光标移到双引号中间,写两个加号,再把光标移到加号中间写上变量。
//System.out.println(userElem.getName());//查看当前节点对象内容
if(userElem!=null){//说明在“数据库”里面找到了用户名和密码。
//登录成功
System.out.println("登录成功");
}else{
//登录失败
System.out.println("登录失败");
}
}
}
How to convert pdf to xml format
How the tcp three-way handshake works
How to buy and sell Bitcoin on Ouyi platform
How to prevent the computer from automatically installing software
How to set up virtual memory
How to recover accidentally deleted files
How to deposit money on Binance
How to check server status