Je suis en train d'analyser le fichier xml. Je reçois toujours NullPointerException
. Quelqu'un peut-il me suggérer où j'ai fait une erreur?Comment obtenir un attribut XML sous une forme particulière
<?xml version="1.0"?>
<categories>
<category name="ABC">
<subcategory name="windows"
loc="C://program files"
link="www.sample.com"
parentnode="Mac"/>
<subcategory name="456"
loc="C://program files"
link="http://"
parentnode="ABC"/>
</category>
<category name="XYZ">
<subcategory name="android"
loc="C://program files"
link="www.sample.com"
parentnode="XYZ"/>
<subcategory name="apple"
loc="C://program files"
link="http://abc.com"
parentnode="XYZ"/>
</category>
</categories>
Dans le fichier xml ci-dessus, je ne souhaite analyser que le nom de la sous-catégorie android. Pour cela, je fait
NodeList catLst = doc.getElementsByTagName("category");
for (int i = 0; i < catLst.getLength(); i++) {
Node cat = catLst.item(i);
NamedNodeMap catAttrMap = cat.getAttributes();
Node catAttr = catAttrMap.getNamedItem("name");
if (catName.equals(catAttr.getNodeValue())) { // CLUE!!!
NodeList subcatLst = cat.getChildNodes();
for (int j = 0; j < subcatLst.getLength(); j++) {
Node subcat = subcatLst.item(j);
NamedNodeMap subcatAttrMap = subcat.getAttributes();
Node subCatAttr = subcatAttrMap.getNamedItem("name");
if (subCatfound.equals(subCatAttr.getNodeValue())
&& subcatAttrMap != null) {
Node subcatAttr = subcatAttrMap.getNamedItem(attrName);
list.add(subcatAttr.getNodeValue());
} else {
System.out.println("NULL");
}
}
}
Chaque fois que je fais cela, je reçois NullPointerException
. Quelqu'un peut-il savoir où j'ai fait une erreur?
Dans quelle ligne obtenez-vous NULLPointerException? – Dimitri
NodeList subcatLst = cat.getChildNodes(); – RAAAAM
@HariRam, avez-vous débuggé (utilisé un débogueur) pour voir ce qui est nul? –