2016-04-27 19 views
0

私はこれまで以上にこの問題を解決しようとしてきましたが、なぜパスワードが認識されないのか分からず、NULLポインタ例外が発生しますこのライン上(userPassword.getTextContent()。等号(パスワード)ここでJava、XMLユーザー検証失敗

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

デバッグとは何ですか? likeはAttributeListのヌルか? – Hamletkrita

答えて

0

属性ノードは、テキストの内容が、値を持っていない方法である場合。あなたは、取得するために構築し、次の使用する必要がありますそれ:

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

また、属性がすでにNodeであるため、org.w3c.dom.Attrにキャストし、.getValue()メソッドを使用できます。

関連する問題