目标:读取整个XML文件并只返回特定值,在这种情况下,返回一个"用户名";以及";密码";,所以我验证用户登录。
问题:当程序读取";users.xml";文件,则它返回"文件中的所有属性和值;用户id=0001";,但是其他子元素,如";用户id=0002";它不会回来了。就像文件中除了第一个元素没有其他内容一样。
我尝试过的:使用for循环逐行读取文件中的所有信息,效果很好,但当涉及到获取一个特定值时,它只能看到第一个值。
我想知道我如何才能做到";getUser(("返回任何";用户名";以及";密码";在文件中?
到目前为止,我拥有的是:
users.xml
<!--I made some modifications in the password field-->
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE Users>
<Users>
<User id = "0001">
<firstname>sinead</firstname>
<lastname>o'connor</lastname>
<email>sinead@oconnor.ie</email>
<username>sinead</username>
<password>oconnor</password>
</User>
<User id= "0002">
<firstname>John</firstname>
<lastname>Don</lastname>
<email>john@don.ie</email>
<username>john</username>
<password>pass</password>
</User>
</Users>
readFile((方法
void readFile(){
String pathXML = "src/user_system/my_library/xml/users.xml";
File usersXML = new File(pathXML);
try {
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = builderFactory.newDocumentBuilder();
Document document = documentBuilder.parse(usersXML);
document.getDocumentElement().normalize();
} catch (ParserConfigurationException | SAXException | IOException e) {
e.printStackTrace();
}
}
getUser((方法
//I just split between readFile() and getUser() for better readability.
//Just changed "Users" to "User".
void getUser(){
NodeList nodeList = document.getElementsByTagName("User");
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
//I also modified this bit.
Element xmlElement = (Element)nodeList.item(i);
String usernameElement = xmlElement.getElementsByTagName("username").item(0)
.getTextContent();
String passwordElement = xmlElement.getElementsByTagName("password").item(0)
.getTextContent();
if (!usernameElement.equals(users.getUsername())){
System.out.println("nUsername "+ """+ users.getUsername()+ """ +
" was not found in our registry." + "nPlease try again");
//users.login() is just a simple username/password form in another class.
users.login();
}
else if(!passwordElement.equals(users.getPassword())){
System.out.println("nWrong password. Please try again.");
users.login();
}else {
System.out.println("Welcome " + WordUtils.capitalize(xmlElement.getElementsByTagName("firstname").item(0)
.getTextContent())+ " " +WordUtils.capitalize(xmlElement.getElementsByTagName("lastname").item(0).getTextContent()));
}
}
}
我更新了代码,但仍然无法获得其他用户,只有第一个用户。我得到以下输出:
Please, choose one of the options below:
1) Press 1 to Log In.
2) Press 2 to Create an Account.
3) Press 3 to Log Off.
> 1
Log In.
Please, enter your username:
> john
Please, enter your password:
> pass
Username "john" was not found in our registry.
感谢您的帮助。
您可以根据标签名称提取所有节点,在这里您使用了Users
作为基本节点,但也可以通过调用来查询User
标签
NodeList nodeList = document.getElementsByTagName("User");
使用上面来自xml文档根的代码,您只有User
节点,一旦您有了所有的User
节点,您就可以逐一迭代它,并查询User节点内部的节点,如下所示。
NodeList nodeList = document.getElementsByTagName("User");
for(int i=0;i<nodeList.getLength();i++) {
Element xmlElement = (Element)nodeList.item(i);
System.out.println(xmlElement.getElementsByTagName("username").item(0).getTextContent());
System.out.println(xmlElement.getElementsByTagName("password").item(0).getTextContent());
}
请注意,在示例xml中,username和password标记具有相同的值,您可能需要更改这些值以验证输出。
一些输出会有所帮助。我的猜测是,当您创建nodelist时,您正在创建一个长度为1的列表,因为";用户";将抓取带有标签"的单个元素;用户";。
NodeList nodeList = document.getElementsByTagName("Users");
您可能想要用填充节点列表
NodeList nodeList = document.getElementsByTagName("User");