我一直试图让这项工作的时间超过我现在想说的,只是无法弄清楚为什么它不会识别密码,我得到一个空的此行上的指针异常if(userPassword.getTextContent()。equals(password)
这是方法
public class XML {
public static boolean login(String email, String password) {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.parse("data.xml");
Element root = document.getDocumentElement();
NodeList nList = root.getChildNodes();
for (int i = 0; i < nList.getLength(); i++) {
Node nNode = nList.item(i);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element users = (Element) nNode;
if (users.getNodeName().compareTo("users") == 0) {
NodeList userList = users.getChildNodes();
for (int j = 0; j < userList.getLength(); j++) {
Node userNode = userList.item(j);
NodeList AttributeList = userNode.getChildNodes();
Node userPassword = AttributeList.item(1);
Node userEmail = AttributeList.item(0);
if (userPassword.getTextContent().equals(password)
&& userEmail.getTextContent().equals(email)) {
return true;
}
}
}
}
}
} catch (ParserConfigurationException | SAXException | IOException e) {
}
return false;
}
答案 0 :(得分:0)
属性节点没有文本内容,只有值。您应该使用以下构造来检索它:
Node userNode = userList.item(j);
String attributeValue = userNode.getAttribute("attributeName")
或者,由于您已经拥有Node
个属性,因此可以将它们转换为org.w3c.dom.Attr
并使用他们的.getValue()
方法。