2013-08-07 78 views
0

有沒有人幫助找到問題,爲什麼沒有XML文件解析與下面的代碼.... xml格式可以看到here解析XML文件有錯誤

我試着用log.i逐行測試(「你的字符串在這裏」,xml);函數,但無法看到循環開始時的代碼執行....

我已經使用Splash活動,其中AsyncTask()函數在後臺執行,然後ListActivity用於顯示所有的DOMParser活動輸出。 ... 因此,任何人幫我從這個問題脫身不惜一切.... 預先感謝您

package com.wfwf.everestnewsapp.parser; 

import java.net.MalformedURLException; 
import java.net.URL; 

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

import org.jsoup.Jsoup; 
import org.jsoup.select.Elements; 
import org.w3c.dom.Document; 
import org.w3c.dom.Node; 
import org.w3c.dom.NodeList; 
import org.xml.sax.InputSource; 

import android.widget.Toast; 

public class DOMParser { 

    private RSSFeed _feed = new RSSFeed(); 

    public RSSFeed parseXml(String xml) { 

     URL url = null; 
     try { 
      url = new URL(xml); 
     } catch (MalformedURLException e1) { 
      e1.printStackTrace(); 
     } 

     try { 
      // Create required instances 
      DocumentBuilderFactory dbf; 
      dbf = DocumentBuilderFactory.newInstance(); 
      DocumentBuilder db = dbf.newDocumentBuilder(); 

      // Parse the xml 
      Document doc = db.parse(new InputSource(url.openStream())); 
      doc.getDocumentElement().normalize(); 

      // Get all <item> tags. 
      NodeList nl = doc.getElementsByTagName("item"); 
      int length = nl.getLength(); 

      for (int i = 0; i < length; i++) { 
       Node currentNode = nl.item(i); 
       RSSItem _item = new RSSItem(); 

       NodeList nchild = currentNode.getChildNodes(); 
       int clength = nchild.getLength(); 

       // Get the required elements from each Item 
       // Ishwor changed the code j=0 and j= j+1 
       for (int j = 0; j < clength; j = j + 1) { 

        Node thisNode = nchild.item(j); 
        String theString = null; 


        /*//ishwor changed as 
        if (thisNode != null && thisNode.getFirstChild() != null) { 
          theString = thisNode.getFirstChild().getNodeValue(); 
         } 
         */ 

        String nodeName = thisNode.getNodeName(); 

        //theString = nchild.item(j).getFirstChild().getNodeValue(); 

        if(nchild.item(j).getFirstChild().getNodeValue()!=null){ 
        //if (theString != null) { 
         //String nodeName = thisNode.getNodeName(); 

         if ("title".equals(nodeName)) { 
          // Node name is equals to 'title' so set the Node 
          // value to the Title in the RSSItem. 
          _item.setTitle(theString); 
         } 

         else if ("description".equals(nodeName)) { 
          _item.setDescription(theString); 

          // Parse the html description to get the image url 
          String html = theString; 
          org.jsoup.nodes.Document docHtml = Jsoup.parse(html); 
          Elements imgEle = docHtml.select("img"); 
          _item.setImage(imgEle.attr("src")); 
         } 

         else if ("pubDate".equals(nodeName)) { 

          // We replace the plus and zero's in the date with 
          // empty string 
          String formatedDate = theString.replace(" +0000", 
            ""); 
          _item.setDate(formatedDate); 
         } 

        } 
       } 

       // add item to the list 
       _feed.addItem(_item); 
      } 

     } catch (Exception e) { 
     } 

     // Return the final feed once all the Items are added to the RSSFeed 
     // Object(_feed). 
     return _feed; 
    } 

} 
+1

您可以共享錯誤日誌? – Lugaru

回答

0
private Document getDocument(InputStream stream) { 
    Document doc = null; 
    try { 
     DocumentBuilderFactory dbFactory = DocumentBuilderFactory 
       .newInstance(); 
     DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); 
     if (stream != null) { 
      doc = dBuilder.parse(stream); 
      doc.getDocumentElement().normalize(); 
     } else { 
     } 

    } catch (ParserConfigurationException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (SAXException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
     // TODO: handle exception 
    } 
    return doc; 
} 

private String getTagValue(String sTag, Element eElement) { 
    String value = ""; 
    try { 
     NodeList nlList = eElement.getElementsByTagName(sTag).item(0) 
       .getChildNodes(); 
     for (int i = 0; i < nlList.getLength(); i++) { 
      Node nValue = nlList.item(i); 
      if (nValue != null) { 
       value = value + nValue.getNodeValue().trim(); 
      } 
     } 
    } catch (Exception e) { 
    } 
    return value; 
} 

public EntityBean parseDoLogin(String requestOutput) { 
    EntityBean response = null; 
    InputStream stream = (InputStream) new ByteArrayInputStream(
      requestOutput.getBytes()); 
    Document doc = getDocument(stream); 
    NodeList nodeList = doc.getElementsByTagName("startTag"); 
    for (int i = 0; i < nodeList.getLength(); i++) { 
     Node nNode = nodeList.item(i); 
     Element iElement = (Element) nNode; 
     response = new EntityBean(); 
     String result = new String(getTagValue("result", iElement)); 
      response.setResult(result); 

     String msg = new String(getTagValue("message", iElement)); 
     response.setMsg(msg); 

    } 

    return response; 
} 
+0

謝謝你的幫助我會盡我所能。 –