空标签在黑莓中解析Xml时返回Null异常

问题描述:

我在解析黑莓中的Xml时遇到了一些问题。 如果Xml包含空标签。 该代码返回一个空例外.. 一些解决方案建议使用尝试和catch.What我应该做什么来解决这个问题?空标签在黑莓中解析Xml时返回Null异常

,这是解析代码

DocumentBuilderFactory docBuilderFactory= DocumentBuilderFactory. newInstance(); 
      DocumentBuilder docBuilder= docBuilderFactory.newDocumentBuilder(); 
      docBuilder.isValidating(); 
      doc = docBuilder.parse(conn.openInputStream()); 

      doc.getDocumentElement().normalize(); 
      list=doc.getElementsByTagName("*"); 
      node=new String(); 
      element = new String(); 

      //this "for" loop is used to extract all elements and their value, so they can be displayed on the device 

      for (int i=0;i<list.getLength();i++){ 
       Node value=list.item(i).getChildNodes().item(0); 
       //getting attribute ==> Node value=list.item(i).getAttributes().item(0); 


       node=list.item(i).getNodeName(); 
       element=value.getNodeValue(); 
       if(node.equals("Name")){ 
       // some code goes here 

空标签,如:< /标签> 或<标签> <标签/>

你的代码的一部分抛出空指针异常?您的代码似乎认为由getElementsByTagName返回的所有结果都将具有子节点,这是有问题的,因为该查询适用于文档中的所有节点。

//文本是您的标签名称。

NodeList _textNdList = doc.getElementsByTagName(Text); 

String result = getXMLTagValue(_textNdList,0); 

public static String getXMLTagValue(NodeList node,int id) 
    { 
     if(node.item(id).getChildNodes().item(0) == null) 
      return ""; 
     else 
      return node.item(id).getChildNodes().item(0).getNodeValue(); 
    } 

它会帮助你。我也面临同样的问题。而且,我也解决使用this.`


`