没有获得使用rss feed获得的节点的Html节点值

时间:2011-05-31 12:27:01

标签: android html rss

这是rss feed的项目元素

 - <item>
          <guid isPermaLink="false">40</guid> 
          <category>Parking</category> 
          <category>Paris - Charles de Gaulle</category> 
          <title>Quick drop-off</title> 
          <description><description>&lt;span style="color: #00437f;"&gt;&lt;strong&gt;Trains &lt;br /&gt;&#xD;
SNCF mainline trains&lt;br /&gt;&#xD;
&lt;/strong&gt;The Paris-Charles de Gaulle SNCF station is situated between terminals 2C-2D and 2E-2F - The station is called "Aéroport Charles de Gaulle 2 - TGV".&#xD;
&lt;p&gt;Getting to or leaving Paris-Charles de Gaulle :&lt;br /&gt;&#xD;
- Terminals ABCDEF - accessible via the travelator&lt;br /&gt;&#xD;
-From Paris - Opéra to Paris-Charles de Gaulle&lt;br /&gt;&#xD;
Stops at the airport :&lt;br /&gt;&#xD;
- Terminals 2A, 2C, 2E-2F, 2D, 2B, 3 and 1&lt;br /&gt;&#xD;
- Terminal 2G is linked to terminal 2C (entrance 4) by the free N2 shuttle &lt;br /&gt;&#xD;
(Average travel time : 15 minutes)&lt;/p&gt;&#xD;
- Terminal 2G is linked to terminal 2C (entrance 4) by the free N2 shuttle &lt;br /&gt;&#xD;
(Average travel time : 15 minutes)&lt;/p&gt;&#xD;
   Fare* : 15 € (Pass Navigo not valid)&#xD;
&lt;p&gt; &lt;/p&gt;&#xD;
&lt;p&gt;&lt;strong&gt;&lt;br /&gt;&#xD;
 &lt;p&gt; &lt;/p&gt;&#xD;
 6 bus lines between Tremblay en France, Goussainville, Sarcelles, Villiers le Bel, Othis, Villeparisis and Paris-Charles de Gaulle airport.&lt;br /&gt;&#xD;
Fares* : Ticket t+, between 1,14 € and 2,28 €&lt;/p&gt;&#xD;
&lt;div class="ll" style="border-top: #dadada 1px solid; ParaRoi=0&amp;amp;ParaProfiling=0&amp;amp;ParaCompte=Adp_mobile" /&gt; &#xD;
</description> 
          <pubDate>Tue, 01 Dec 2009 00:00:00 +0100</pubDate> 
          <enclosure url="http://services.aeroportsdeparis.fr/iPhoneRessources/pict_DpMinute.png" type="image/png" /> 
          <nbArticles>8</nbArticles> 
          <idSite>0</idSite> 
          </item>

我可以使用除了描述数据之外的Dom解析器访问该父节点下的其他节点的数据。

使用以下功能

    public class Rss_Data_Retriever 
{

    Document myDoc;
    private String[][] data=null;

    public Rss_Data_Retriever(String Url)
    {
        InputStream in;
        in=getInputStream(Url);
        myDoc=getDocument(in);

    }   
    public String getSampleString() 
    {
        String s=getNthNodeValue("category", this.myDoc,0);
        FeastingOfData(this.myDoc, "item");
        return s;
    }

    public InputStream getInputStream(String urlString) 
    {
        int responseCode=0;
        HttpURLConnection  httpurlConnection = null;

        try 
        {
            URL url=new URL(urlString);
            URLConnection connection;


            connection=url.openConnection();

            httpurlConnection=(HttpURLConnection)connection;

            responseCode=httpurlConnection.getResponseCode();

        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        if(responseCode==HttpURLConnection.HTTP_OK)
        {

            Log.d("Message", "httpOk");
            InputStream input_fn=null;
            try
            {
                input_fn = httpurlConnection.getInputStream();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return input_fn;

        }   
        else
            Log.d("Message", "httpNotOk");

        return null;

    }

    public Document getDocument(InputStream inputStream) 
    {
        DocumentBuilder builder;
        NodeList docTitle = null;
        Document Doc=null;


            try 
            {
                builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
                Doc= builder.parse(inputStream);
            }
            catch (ParserConfigurationException e) 
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            catch (FactoryConfigurationError 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 Doc;
    }

    public String getNthNodeValue(String Tag,Document doc,int index) 
    {
        DocumentBuilder builder;
        NodeList docTitle = null;
        Document Doc=null;

        docTitle=doc.getElementsByTagName(Tag);


        builder=null;
        Doc=null;
        Element value=(Element) docTitle.item(index);
        return value.getFirstChild().getNodeValue();

    }
    public String getNthNodeValue(String Tag,Element element,int index,boolean isValue) 
    {

        NodeList nodeList = null;
        try         
        {


            nodeList=element.getElementsByTagName(Tag);
        }
        catch (FactoryConfigurationError e) 
        {

        }

        Element value=(Element) nodeList.item(index);
        if(isValue)
            return value.getFirstChild().getNodeValue();
        else
            return value.getAttribute("url"); 
    }

    public String[][] getData() 
    {
        return data;
    }

    public void FeastingOfData(Document doc,String TagName) 
    {
        DocumentBuilder builder;
        NodeList nodeList = null;




                nodeList=doc.getElementsByTagName(TagName);
                Log.d("Message", "Length:"+nodeList.getLength());

                if(data==null)
                    data=new String [nodeList.getLength()][5];

                int i=0;

                for(i=0;i<nodeList.getLength();i++) 
                {
                    Element node=(Element) nodeList.item(i);
                    String maincategory=getNthNodeValue("category", node, 0,true);
                    String category=getNthNodeValue("category", node, 1,true);
                    String title=getNthNodeValue("title", node, 0,true);
                    String description=getNthNodeValue("description", node, 0,true);
                    String enclosureUrl=getNthNodeValue("enclosure", node, 0,false);


                    data[i][0]=maincategory;
                    data[i][1]=category;
                    data[i][2]=title;
                    data[i][3]=description;
                    data[i][4]=enclosureUrl;



                    Log.d("DataChecked", "maincategory:"+maincategory);
                    Log.d("DataChecked", "category:"+category);
                    Log.d("DataChecked", "title"+title);
                    Log.d("DataChecked", "enclosureUrl:"+enclosureUrl);
                    Log.d("DataChecked", "Description:"+description);
                }

    }
}

我尝试打印描述的节点值然后我得到了“&lt;”简单只有在我可以打印的同时 类别,标题等。

可能是什么问题

此致 Kariyachan

2 个答案:

答案 0 :(得分:1)

您的描述包含更多xml / html标记,但未包含CDATA声明。我的猜测是,这导致解析器将内容解释为进一步的标记而不是节点值。您可以找到CDATA here的基本说明。

答案 1 :(得分:1)

这就是我解决Html内容在 description 标签中的方式所以我改变了代码如下。现在我可以 将返回的字符串加载到webview。

希望有人会发现这有用。

由于

public String getNthNodeValue(String Tag, Element element, int index,
            boolean isValue) {

        NodeList nodeList = null;
        try {

            nodeList = element.getElementsByTagName(Tag);
        } catch (FactoryConfigurationError e) {

        }

        Element value = (Element) nodeList.item(index);
        if (Tag.equals("description")) {
            String str = "";
            value.normalize();
            str = value.getFirstChild().getNodeValue();
            return str;
        }

        if (isValue)
            return value.getFirstChild().getNodeValue();
        else
            return value.getAttribute("url");
    }