2013-04-29 4 views
0

<image> 태그에서 이미지 URL을 구문 분석하기위한 코드를 작성했습니다. 이미지 태그에서 이미지 URL을 파싱하는 문제

코드

은 다음과 같습니다 :

NodeList imageLink = docElement.getElementsByTagName("image"); 
      String nUrl; 
      if (imageLink.toString() != null) { 
       Element element = (Element) imageLink.item(0); 
       NodeList imageUrl = element.getElementsByTagName("url"); 
       if (imageUrl.toString() != null) { 
        Element imageFirst = (Element) imageUrl.item(0); 
        nUrl = imageFirst.getFirstChild().getNodeValue(); 
        Log.d(TAG, 
          "<<<<<<<<<<<<<<<<<<<<<<..............Image Url is : " 
            + nUrl 
            + ".....................>>>>>>>>>>>>>>>>>....."); 
       } else { 
        Log.d(TAG, 
          "<<<<<<<<<<<<<<<<<<<<<<..............Image Url is null : .....................>>>>>>>>>>>>>>>>>....."); 
        nUrl = "http://static.dnaindia.com/images/710/logo_dna_rss.gif"; 
       } 
      } else { 
       Log.d(TAG, 
         "<<<<<<<<<<<<<<<<<<<<<<..............Image tag is not found.....................>>>>>>>>>>>>>>>>>....."); 
       nUrl = "http://static.dnaindia.com/images/710/logo_dna_rss.gif"; 
      } 

IT는 rss feed which having <image> tag와 함께 잘 작동했다. <image> URL이없는 Rss의 기본 이미지를 설정하고 싶습니다.

내 코드는 NodeList imageUrl = element.getElementsByTagName("url");java.lang.NullPointerException으로 표시됩니다.

NodeList에 대해 null을 확인하는 방법은 무엇입니까?

그리고 그것을 수정하는 아이디어.

미리 감사드립니다.

+0

그것은 널 (null) 인'element' 객체입니다. 'if (null! = element)'가 할 것입니다. – 1615903

답변

0

try-catch를 사용하여 yy 코드 을 서라운드하고 'url 태그가 발견되지 않으면 예외를 catch합니다. 이

try{ 
    NodeList imageUrl = element.getElementsByTagName("url"); 
} catch(NullPointerException e){ 
    nUrl = "http://static.dnaindia.com/images/710/logo_dna_rss.gif"; 
    e.printStackTrace(); 
} 

희망이 도움이처럼

...

+0

답장을 보내 주셔서 감사합니다 – Dhasneem

+0

도와 드리겠습니다. :) –

관련 문제