There is an XML file:

<?xml version="1.0" encoding="UTF-8"?> <studentsList> <student> <code>0</code> <lastName>bessonov</lastName> <firstName>leonid</firstName> <patronymic>valentinovich</patronymic> <phone>89061234567</phone> </student> <student> <code>1</code> <lastName>petrov</lastName> <firstName>leonid</firstName> <patronymic>valentinovich</patronymic> <phone>89000000000</phone> </student> </studentsList> 

You must compare the last last name in $ lastName with the lastName fields in the file.

  NodeList nodeList = doc.getElementsByTagName("student"); for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); if(node.getChildNodes().item(1).getNodeValue().equals($lastName) == true){ } 

Such if gives me a NullPointerException. Google hasn't helped yet.

    1 answer 1

    getNodeValue() returns the value of the node. The concept of "value" is defined only for certain types of nodes, for example, attributes. Element does not matter. It has textual content that can be obtained using the getTextContent() method. As a result, the code will turn out to be

     if(node.getChildNodes().item(1).getTextContent().equals($lastName)){