Null Pointer Exception in XMl Parsing

前端 未结 4 699
礼貌的吻别
礼貌的吻别 2021-01-13 23:00

I need to parser an Xml Document and store the values in Text File, when i am parsing normal data (If all tags have data) then its working fine, but if any tag doesnt have d

4条回答
  •  灰色年华
    2021-01-13 23:29

    This code should work:

    // getNode function
    private static String getNode(String sTag, Element eElement) {
        //if sTag exists(not null) I get childNodes->nlList
        if (eElement.getElementsByTagName(sTag).item(0)!=null){
            NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();
            //check if child (nlList) contain something
            if ((nlList.getLength() == 0))//if the content is null
                return "";
            //if child contains something extract the value of the first element
            Node nValue = (Node) nlList.item(0);
                return nValue.getNodeValue();
        }
        return "";
    }
    

提交回复
热议问题