Q:

How can I use "for" loop and "if" loop both together to verify the existence of xml elements in xml document

I'm trying to make a login system. For that I need to check existence of xml elements ("username" and "password") in xml document. If match the "username" and "password" need to return true, otherwise false. For that I use "for" loop to go through the entire document and use "if" loop for validation. I'm using java and dom parser.  
xml document:
<login>
<user>
    <username>sunimal</username>
    <password>1111</password>
</user>
<user>
    <username>naveen</username>
    <password>852</password>
</user>
<user>
    <username>chamara</username>
    <password>12345</password>
</user></login>

java code:
boolean a = false;
    try {
        File inputFile = new File("C:\\Users\\Kavindu\\Documents\\NetBeansProjects\\chatserver2\\xml\\userrecord2.xml");
        DocumentBuilderFactory dbFactory
                = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(inputFile);

        NodeList userList = doc.getElementsByTagName("login");

          for (int i = 0; i < userList.getLength(); i++) {
            Node user = userList.item(i);
            Element eElement = (Element) user;

            if (eElement.getElementsByTagName("username").item(i).getTextContent().equals(username) && eElement.getElementsByTagName("password").item(i).getTextContent().equals(password)) {

                a = true;
            } else {
                a = false;
            }
        }

    } catch (Exception ex) {

    }
    return a;
}

If I input first elements("sunimal" and "1111"),I can get true, but for other elements return false. I tried "for" loop and "if" loop in different ways, but I couldn't get the result.I'm newly with programming.

A:

Here is your solution; I have checked this in my IDE. Thanks. :-)
to get the first element of a tag you need to use index 0.  eElement.getElementsByTagName("username").item(0) 
When you have matched user name and password you need to get out from the loop,
a = true;
  break;
otherwise in the next iteration it will set to false again. 
public boolean checkLogin(){

boolean a = false;
try {
    File inputFile = new File("C:\\Users\\Kavindu\\Documents\\NetBeansProjects\\chatserver2\\xml\\userrecord2.xml");
    DocumentBuilderFactory dbFactory
            = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(inputFile);
    NodeList userList = doc.getElementsByTagName("user");
    System.out.println("userlist length :: "+ userList.getLength());
      for (int i = 0; i < userList.getLength(); i++) {
        Node user = userList.item(i);
        Element eElement = (Element) user;
        if (eElement.getElementsByTagName("username").item(0).getTextContent().equals(username) && eElement.getElementsByTagName("password").item(0).getTextContent().equals(password)) {

            a = true;
            break;
        } else {
            a = false;
        }
    }

} catch (Exception ex) {
    System.out.println(ex.getMessage());

}
return a;

}

