2012-03-30 8 views
0

ブラックベリーでXmlを解析中に問題が発生します。 Xmlに空のタグが含まれている場合。 コードでNull例外が返されます。 いくつかのソリューションはtryとcatchを使用することを提案しています。この問題を解決するにはどうすればよいですか?ブラックベリーで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 

あるような空のタグ:< /タグ> または<タグ> <タグであるあなたのコードの一部/>

答えて

0

NULLポインタ例外をスローしますか?あなたのコードは、getElementsByTagNameによって返されたすべての結果が子ノードを持つと仮定しているように見えます。これは、ドキュメント内のすべてのノードに対するクエリなので問題があります。

0

//テキストはタグ名です。

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.`


`