try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
final Document doc = db.parse(new File("config.xml"));
for (Node n = doc.getFirstChild(); n != null; n = n.getNextSibling()) {
for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling()) {
for (Node m = d.getFirstChild(); m != null; m = m.getNextSibling()) {
//изменить по возможности этот кусок кода.
String stringValue;
if ("variable".equals(m.getNodeName())) {
stringValue = m.getAttributes().getNamedItem("storiesNumber").getNodeValue().trim();
System.out.println(stringValue);
stringValue = m.getAttributes().getNamedItem("elevatorCapacity").getNodeValue().trim();
System.out.println(stringValue);
}
}
}
}
}
catch (Exception e) {
System.out.println (e.getClass().getName() + " with message : " + e.getMessage());
e.printStackTrace();
}
25
java.lang.NullPointerException with message : null
java.lang.NullPointerException
<?xml version="1.0" encoding="UTF-8"?>
<Lift>
<variables>
<variable storiesNumber = "25"/>
<variable elevatorCapacity = "30"/>
<variable passengerNumber = "13"/>
<variable animationBoost = "2"/>
</variables>
<variable storiesNumber = "25" elevatorCapacity = "30"/>