I did not notice any of the answers regarding your last note on NPE when trying to access attributes.
I also get NPE to access the attributes of the nodes, so I assume something is terribly wrong.
Since I saw the following sentence on multiple sites, I assume this is the usual way to access attributes:
String myPropValue = node.getAttributes().getNamedItem("myProp").getNodeValue();
which works fine if the nodes always contain the myProp
attribute, but if it has no attributes, getAttributes
returns null. Also, if there are attributes but not the myProp
attribute, getNamedItem
returns null.
I am currently using
public static String getStrAttr(Node node, String key) { if (node.hasAttributes()) { Node item = node.getAttributes().getNamedItem(key); if (item != null) { return item.getNodeValue(); } } return null; } public static int getIntAttr(Node node, String key) { if (node.hasAttributes()) { Node item = node.getAttributes().getNamedItem(key); if (item != null) { return Integer.parseInt(item.getNodeValue()); } } return -1; }
in the utility class, but your mileage may vary.
source share