2011-09-01 69 views
0

我現在面臨的一個特殊問題,同時解析XML所以解析器正常工作,直到它encouters < URL />標籤,它不包含任何價值,我已經取得的測試IL我的代碼:Android的DOM解析器錯誤

static final String ImageHotel = "Url"; 

...

else if (name.equalsIgnoreCase("ImageHotel")){ 
         message.setHotelImage(property.getFirstChild().getNodeValue()); 
          if (!marchand.getImgHtlUrl().equalsIgnoreCase("")){ 
           message.setHotelImageLink(new URL(marchand.getImgHtlUrl() + property.getFirstChild().getNodeValue())); 
          }else{ 
           message.setHotelImageLink(new URL("http://localhost/noimage.jpg")); 
          } 
         } 

此保持拋出錯誤,甚至當我試圖用的try/catch周圍繞過..

任何幫助是值得歡迎

謝謝。 Houssem。

回答

0

我告訴過你,try/catch周圍沒有工作,所以,現在它的確如此,因爲我不得不跟蹤NullPointerException異常而不是簡單的異常的,就像這樣:

try{ 
     message.setHotelImage(property.getFirstChild().getNodeValue()); 
    }catch(NullPointerException nEx){ 
     marchand.setImgHtlUrl(""); 
    } 

感謝你們和遺憾,這個警報;)

1

我有參考解析這個文件,嘗試添加你自己的數據,並獲取標記值....

import java.io.BufferedReader; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.net.URL; 
import java.net.URLConnection; 
import java.util.Vector; 

import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory; 

import org.w3c.dom.Document; 
import org.w3c.dom.Element; 
import org.w3c.dom.Node; 
import org.w3c.dom.NodeList; 

public class NewsParsing { 

    NewsBeen Objnewsbeen; 
    Vector<NewsBeen> vectParse; 

    public NewsParsing() { 
     System.out.println("Constructor is calling Now ..."); 

     try { 

      vectParse = new Vector<NewsBeen>(); 
      // http://www.npr.org/rss/rss.php?id=1001 
      URL url = new URL(
        "http://www.npr.org/rss/rss.php?id=1001"); 
      URLConnection con = url.openConnection(); 

      System.out.println("Connection is : " + con); 

      BufferedReader reader = new BufferedReader(new InputStreamReader(
        con.getInputStream())); 
      System.out.println("Reader :" + reader); 

      String inputLine; 
      String fullStr = ""; 
      while ((inputLine = reader.readLine()) != null) 
       fullStr = fullStr.concat(inputLine + "\n"); 

      InputStream istream = url.openStream(); 

      DocumentBuilder builder = DocumentBuilderFactory.newInstance() 
        .newDocumentBuilder(); 

      System.out.println("Builder : " + builder); 

      Document doc = builder.parse(istream); 

      System.out.println("Doc is : " + doc); 

      doc.getDocumentElement().normalize(); 
      System.out.println("After Normlize : " + doc); 

      System.out.println("Root is : " 
        + doc.getDocumentElement().getNodeName()); 

      System.out 
        .println("-------------------------------------------------------------------------------------------------------------"); 
      Element element = doc.getDocumentElement(); 

      parseFile(element); 

      for (int index1 = 0; index1 < vectParse.size(); index1++) { 
       NewsBeen ObjNB = (NewsBeen) vectParse.get(index1); 

       System.out.println("Item No : " + index1); 
       System.out.println(); 

       System.out.println("Title is : " + ObjNB.title); 
       System.out.println("Description is : " + ObjNB.description); 
       System.out.println("Pubdate is : " + ObjNB.pubdate); 
       System.out.println("Link is : " + ObjNB.link); 
       System.out.println("Guid is : " + ObjNB.guid); 

       System.out.println(); 
       System.out 
         .println("-------------------------------------------------------------------------------------------------------------"); 

      } 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

    } 

    private void parseFile(Node node) { 

     NodeList nodelist = node.getChildNodes(); 

     for (int index = 0; index < nodelist.getLength(); index++) { 
      Node nodefromList = nodelist.item(index); 

      if (nodefromList.getNodeType() == Node.ELEMENT_NODE) { 
       // System.out.println("node.getNodeType() : " + 
       // nodefromList.getNodeType()); 
       // System.out.println("Node is : " + node.getNodeName()); 

       if (nodefromList.getNodeName().equalsIgnoreCase("item")) { 

        Objnewsbeen = new NewsBeen(); 
        vectParse.addElement(Objnewsbeen); 
       } 

       if (nodefromList.hasChildNodes()) { 
        if (nodefromList.getChildNodes().item(0).getNodeName() 
          .equals("#text")) { 

         if (!nodefromList.getChildNodes().item(0) 
           .getNodeValue().trim().equals("") 
           && Objnewsbeen != null) 

          if (nodefromList.getNodeName().equalsIgnoreCase(
            "title")) { 
           Objnewsbeen.title = nodefromList 
             .getChildNodes().item(0).getNodeValue(); 

          } else if (nodefromList.getNodeName() 
            .equalsIgnoreCase("description")) { 
           Objnewsbeen.description = nodefromList 
             .getChildNodes().item(0).getNodeValue(); 

          } else if (nodefromList.getNodeName() 
            .equalsIgnoreCase("pubDate")) { 
           Objnewsbeen.pubdate = nodefromList 
             .getChildNodes().item(0).getNodeValue(); 

          } else if (nodefromList.getNodeName() 
            .equalsIgnoreCase("link")) { 
           Objnewsbeen.link = nodefromList.getChildNodes() 
             .item(0).getNodeValue(); 

          } else if (nodefromList.getNodeName() 
            .equalsIgnoreCase("guid")) { 
           Objnewsbeen.guid = nodefromList.getChildNodes() 
             .item(0).getNodeValue(); 

          } else { 
           // System.out.println(); 
          } 
        } 
        parseFile(nodefromList); 
       } 
      } 
     } 

    } 

    public static void main(String[] args) { 
     new NewsParsing(); 
    } 

} 

的NewsBeen類::

公共類NewsBeen {

public String title; 
public String description; 
public String pubdate; 
public String link; 
public String guid; 

}

+0

它工作正常這種方式,但我的代碼幾乎完全寫入,並準備等等做這樣的改變會浪費時間。看到我找到的解決方案,下一篇文章。 – Houssem