【问题标题】:Java , XML User Verification FailureJava,XML用户验证失败
【发布时间】:2016-08-21 11:58:53
【问题描述】:

我一直在尝试使这项工作的时间比我现在想说的要长,只是无法弄清楚为什么它无法识别密码,如果 (userPassword .getTextContent().equals(密码)

方法如下

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;
}

【问题讨论】:

  • 你的调试说明了什么?比如 AttributeList 是 null 还是?

标签: java xml


【解决方案1】:

属性节点没有文本内容,而是一个值。您应该使用以下构造来检索它:

Node userNode = userList.item(j);
String attributeValue = userNode.getAttribute("attributeName")

或者,既然您已经拥有Nodes 属性,您可以将它们转换为org.w3c.dom.Attr 并使用它们的.getValue() 方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-23
    • 1970-01-01
    • 2016-11-06
    • 2019-05-18
    • 1970-01-01
    • 1970-01-01
    • 2017-07-25
    • 2012-05-31
    相关资源
    最近更新 更多