2011-08-03 2 views
0

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?

+0

Dans quelle ligne obtenez-vous NULLPointerException? – Dimitri

+0

NodeList subcatLst = cat.getChildNodes(); – RAAAAM

+0

@HariRam, avez-vous débuggé (utilisé un débogueur) pour voir ce qui est nul? –

Répondre

2

Ce code est une simplification de ce que vous essayez d'essayer d'atteindre:

public static Element getElementByNameAttribute(String elementName, String nameAttributeValue, Document doc) { 
    if (elementName!= null && !elementName.isEmpty() && nameAttributeValue!= null && !nameAttributeValue.isEmpty()) { 

     NodeList subCategoryList = doc.getElementsByTagName(elementName); 
     for (int i = 0; i < subCategoryList.getLength(); i++) { 
      Element element = (Element) subCategoryList.item(i); 

      if (nameAttributeValue.equals(element.getAttribute("name"))) { 
       return element; 
      } 
     } 
    } 

    return null; 
} 

Si vous mettez cela dans une classe, par exemple DOMUtil (dans mon cas), vous pouvez simplement faire ceci:

Element subCategoryAndroid = DOMUtil.getElementByNameAttribute("subcategory", "android", doc); 

PS: Ceci est non testé.

Questions connexes