2013-11-28 46 views
2

我无法从RSS数据...空例外 我从这个link检索,它只有如何从rss获取数据?

<?xml version="1.0" encoding="utf-8"?> 
<GoldQuotes> 
    <Price Date="2013-11-28 09:22" Value="1244.30" /> 
    <Price Date="2013-11-28 09:20" Value="1243.10"/> 
    <Price Date="2013-11-28 09:18" Value="1243.30"/> 
    [...] 
</GoldQuotes> 

这是我的Java代码片断

if (responseCode == HttpURLConnection.HTTP_OK) { 
    InputStream in = httpConnection.getInputStream(); 
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
    DocumentBuilder db = dbf.newDocumentBuilder(); 
    Document dom = db.parse(in); 
    Element docEle = dom.getDocumentElement(); 

    NodeList nl = docEle.getElementsByTagName("GoldQuotes"); //*** 

    if ((nl != null) && (nl.getLength() > 0)) { 
     for (int i = 0; i < nl.getLength(); i++) { 

       dissectNode(nl, i); 
     } 
    }//if 

[...] 

public void dissectNode(NodeList nl, int i) { 
    try { 

      Element entry = (Element) nl.item(i); 
     Element price = (Element) entry.getElementsByTagName("Price").item(0); 
     String priceValue = price.getAttribute("Value"); //get gold value 

     SingleNewsItem singleItem = new SingleNewsItem(priceValue); 
     newsList.add(singleItem); 

     } catch (DOMException e) { 
      e.printStackTrace(); 
     } 
    }// dissectNode 

我做NodeList nl = docEle.getElementsByTagName("GoldQuotes")后。我与nl.getLength()测试,其中返回0 .. 我错过了什么?

+0

'GoldQuotes'是文档根元素,通过'getDocumentElement()'返回;在id里面没有嵌套的GoldQuotes元素,所以'docEle.getElementsByTagName(“GoldQuotes”)'正确地返回一个空的列表 –

+0

所以我想我该怎么办? – lynndragon

回答

1

您试图解析文档不是RSS,只是一个自定义的XML格式。当您解析它,它足以改变这样我们的代码:

// this returns the root element, ie "GoldQuotes" 
Element docEle = dom.getDocumentElement(); 

// get the list of Price elements children of the root 
NodeList nl = docEle.getElementsByTagName("Price"); 

if ((nl != null) && (nl.getLength() > 0)) { 
    for (int i = 0; i < nl.getLength(); i++) { 
     // get the i-th Price element from the nodelist 
     Element entry = (Element) nl.item(i); 
     // get its Value attribute 
     String priceValue = entry.getAttribute("Value"); 
     [....] 
    } 

}

+0

whoa..it works ..我与GoldQuotes和Price混淆..这是因为我的错......它修复:)我现在可以睡觉了:PI想给你的名声,但我需要更多的声誉..所以我不能。 – lynndragon

+0

欢迎,乐意帮忙 –

+0

我可以跟你先生朋友Mr.guido吗?我需要一个像你这样的老师:) – lynndragon

1

试试这个..

import java.io.IOException; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.util.ArrayList; 

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

import org.w3c.dom.Document; 
import org.w3c.dom.Element; 
import org.w3c.dom.Node; 
import org.w3c.dom.NodeList; 
import org.xml.sax.InputSource; 
import org.xml.sax.SAXException; 

import android.os.AsyncTask; 
import android.os.Build; 
import android.os.Bundle; 
import android.annotation.TargetApi; 
import android.app.Activity; 
import android.app.ProgressDialog; 
import android.view.Menu; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 

public class MainActivity extends Activity { 

    String url = "https://www.igolder.com/GoldData.ashx?type=Historical&hours=24&currency=USD&tz=UTC&unit=oz&output=xml"; 
    ProgressDialog pDialog; 


    @TargetApi(Build.VERSION_CODES.HONEYCOMB) 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main);  

     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) 
      new XmlParsing(url).executeOnExecutor(
        AsyncTask.THREAD_POOL_EXECUTOR, new String[] { null }); 
     else 
      new XmlParsing(url).execute(new String[] { null }); 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    public class XmlParsing extends AsyncTask<String, Void, String> { 

     // variables passed in: 
     String urls; 

     // constructor 
     public XmlParsing(String urls) { 
      this.urls = urls; 
     } 

     @Override 
     protected void onPreExecute() { 
      pDialog = ProgressDialog.show(NetActivity.this, 
        "Fetching Details..", "Please wait...", true); 
     } 

     @Override 
     protected String doInBackground(String... params) { 
      // TODO Auto-generated method stub 

      URL url; 
      try { 

       url = new URL(urls); 
       DocumentBuilderFactory dbf = DocumentBuilderFactory 
         .newInstance(); 
       DocumentBuilder db = dbf.newDocumentBuilder(); 
       Document doc = db.parse(new InputSource(url.openStream())); 

       doc.getDocumentElement().normalize(); 

       NodeList nodeList = doc.getElementsByTagName("GoldQuotes"); 

       for (int i = 0; i < nodeList.getLength(); i++) { 

        Node node = nodeList.item(i); 

        Element fstElmnt = (Element) node; 
        NodeList nameList = fstElmnt.getElementsByTagName("Price"); 
        Element nameElement = (Element) nameList.item(0); 
        nameList = nameElement.getChildNodes(); 
        String priceValue = nameElement.getAttribute("Value"); //get gold value 

        SingleNewsItem singleItem = new SingleNewsItem(priceValue); 
        newsList.add(singleItem); 

        System.out.println("Value : " 
          + (nameElement.getAttribute("Value"))); 
       } 

      } catch (MalformedURLException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 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(); 
      } 

      return null; 
     } 

     @Override 
     protected void onPostExecute(String result) { 
      // Now we have your JSONObject, play around with it. 
      if (pDialog.isShowing()) 
       pDialog.dismiss(); 
     } 

    } 
}