如何检索RSS源? [英] How to retrieve RSS feed?

查看:106
本文介绍了如何检索RSS源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从链接 app2.nea.gov.sg/data/rss/nea_psi.xml 检索标题和描述。这是一个RSS feed。我怎样才能做到这一点?

我的code:

MainActivity.java

 公共类MainActivity延伸活动{    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);        / **创建一个新的布局,以显示视图* /
        的LinearLayout布局=新的LinearLayout(本);
        layout.setOrientation(1);        / **创建一个新的TextView阵列来显示结果* /
        TextView的名称[];
        TextView的网站[];
        尝试{            网址URL =新的URL(
                    http://app2.nea.gov.sg/data/rss/nea_psi.xml);
            DBF的DocumentBuilderFactory = DocumentBuilderFactory.newInstance();
            的DocumentBuilder分贝= dbf.newDocumentBuilder();
            文档的文档= db.parse(新的InputSource(url.openStream()));
            doc.getDocumentElement()正常化()。            节点列表节点列表= doc.getElementsByTagName(项目);            / **指定由ArrayList的大小TextView的阵列lenght * /
            NAME =新的TextView [nodeList.getLength()];
            网站=新的TextView [nodeList.getLength()];
            的for(int i = 0; I< nodeList.getLength();我++){                节点node = nodeList.item(ⅰ);                名称[i] =新的TextView(本);
                网站由[i] =新的TextView(本);
                元素fstElmnt =(元素)节点;
                节点列表名称列表=((文件)fstElmnt).getElementsByTagName(称号);
                元件nameElement =(元件)nameList.item(0);
                NAMELIST =((节点)nameElement).getChildNodes();
                名称[I] .setText(称号=
                        +((节点)nameList.item(0))getNodeValue())。                节点列表websiteList =((文件)fstElmnt).getElementsByTagName(说明);
                元件websiteElement =(元件)websiteList.item(0);
                websiteList =((节点)websiteElement).getChildNodes();
                网站[I] .setText(描述=
                        +((节点)websiteList.item(0))getNodeValue())。                layout.addView(名称[I]);
                layout.addView(网站[I]);
            }
        }赶上(例外五){
            的System.out.println(XML帕兴错误时抛出=+ e)条;
        }        / **设置布局视图中显示* /
        的setContentView(布局);    }
    @覆盖
    公共布尔onCreateOptionsMenu(菜单菜单){
        //充气菜单;如果是present这增加了项目操作栏。
        。getMenuInflater()膨胀(R.menu.main,菜单);
        返回true;
    }}

activity_main.xml中

 <的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:paddingBottom会=@扪/ activity_vertical_margin
    机器人:paddingLeft =@扪/ activity_horizo​​ntal_margin
    机器人:paddingRight =@扪/ activity_horizo​​ntal_margin
    机器人:paddingTop =@扪/ activity_vertical_margin
    工具:上下文=MainActivity。>    <的TextView
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字=@字符串/参考hello world/>        <的TextView
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:名字=@ + ID /结果
    />< / RelativeLayout的>


解决方案

 进口的java.net.URL;
进口的java.util.ArrayList;
进口javax.xml.parsers.DocumentBuilder中;
进口javax.xml.parsers.DocumentBuilderFactory中;进口org.w3c.dom.Document中;
进口org.w3c.dom.Element中;
进口org.w3c.dom.Node中;
进口org.w3c.dom.NodeList;
进口org.xml.sax.InputSource中;进口android.app.Activity;
进口android.content.Context;
进口android.os.Bundle;
进口android.text.Html;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.BaseAdapter;
进口android.widget.ListView;
进口android.widget.TextView;公共类XMLParsingDOMExample延伸活动{    ArrayList的<串GT;标题;
    ArrayList的<串GT;描述;    公众的TextView title_text;
    公众的TextView des_text;    // ItemAdapter适配器1;
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.mainpage_listitem_activity);        // ListView控件列表=(ListView控件)findViewById(R.id.list);
        标题=新的ArrayList<串GT;();
        说明=新的ArrayList<串GT;();        title_text =(的TextView)findViewById(R.id.title_text);
        des_text =(的TextView)findViewById(R.id.des_text);        尝试{            网址URL =新的URL(
                    http://app2.nea.gov.sg/data/rss/nea_psi.xml);
            DBF的DocumentBuilderFactory = DocumentBuilderFactory.newInstance();
            的DocumentBuilder分贝= dbf.newDocumentBuilder();
            文档的文档= db.parse(新的InputSource(url.openStream()));
            doc.getDocumentElement()正常化()。            节点列表节点列表= doc.getElementsByTagName(项目);
            的for(int i = 0; I< nodeList.getLength();我++){                节点node = nodeList.item(ⅰ);                元素fstElmnt =(元素)节点;
                节点列表名称列表= fstElmnt.getElementsByTagName(标题);
                元件nameElement =(元件)nameList.item(0);
                NAMELIST = nameElement.getChildNodes();                title.add(+((节点)nameList.item(0))getNodeValue());                节点列表websiteList = fstElmnt.getElementsByTagName(说明);
                元件websiteElement =(元件)websiteList.item(0);
                websiteList = websiteElement.getChildNodes();                description.add(+((节点)websiteList.item(0))getNodeValue());            }
        }赶上(例外五){
            的System.out.println(XML帕兴错误时抛出=+ e)条;
        }
        title_text.setText(+ title.get(0));        字符串TEMP = Html.fromHtml(description.get(0))的toString()。
        串[] = temp.split(\\\\));
        des_text.setText(+ A [0] +));
    }

mainpage_listitem_activity.xml

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:方向=垂直
     >
            <的TextView
                机器人:ID =@ + ID / title_text
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:文字=标题
                机器人:layout_margin =5DP
                机器人:TEXTSIZE =22dp
                机器人:文字颜色=#FFFFFF/>             <的TextView
                机器人:ID =@ + ID / des_text
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:比重=中心
                机器人:文字=说明
                机器人:layout_margin =5DP
                机器人:TEXTSIZE =18dp
                机器人:文字颜色=#FFFFFF/>
< / LinearLayout中>

I would like to retrieve the title and description from the link app2.nea.gov.sg/data/rss/nea_psi.xml. It's an RSS feed. How can I do this?

my code:

MainActivity.java

public class MainActivity extends Activity {

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

        /** Create a new layout to display the view */
        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(1);

        /** Create a new textview array to display the results */
        TextView name[];
        TextView website[];


        try {

            URL url = new URL(
                    "http://app2.nea.gov.sg/data/rss/nea_psi.xml");
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(new InputSource(url.openStream()));
            doc.getDocumentElement().normalize();

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

            /** Assign textview array lenght by arraylist size */
            name = new TextView[nodeList.getLength()];
            website = new TextView[nodeList.getLength()];


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

                Node node = nodeList.item(i);

                name[i] = new TextView(this);
                website[i] = new TextView(this);


                Element fstElmnt = (Element) node;
                NodeList nameList = ((Document) fstElmnt).getElementsByTagName("title");
                Element nameElement = (Element) nameList.item(0);
                nameList = ((Node) nameElement).getChildNodes();
                name[i].setText("title = "
                        + ((Node) nameList.item(0)).getNodeValue());

                NodeList websiteList = ((Document) fstElmnt).getElementsByTagName("description");
                Element websiteElement = (Element) websiteList.item(0);
                websiteList = ((Node) websiteElement).getChildNodes();
                website[i].setText("description  = "
                        + ((Node) websiteList.item(0)).getNodeValue());



                layout.addView(name[i]);
                layout.addView(website[i]);


            }
        } catch (Exception e) {
            System.out.println("XML Pasing Excpetion = " + e);
        }

        /** Set the layout view to display */
        setContentView(layout);

    }


    @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;
    }

}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

        <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:name ="@+id/result"
    />

</RelativeLayout>

解决方案

import java.net.URL;
import java.util.ArrayList;
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;
import org.xml.sax.InputSource;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class XMLParsingDOMExample extends Activity {

    ArrayList<String> title;
    ArrayList<String> description;

    public TextView title_text;
    public TextView des_text;

    //ItemAdapter adapter1;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mainpage_listitem_activity);

        //ListView list = (ListView) findViewById(R.id.list);
        title = new ArrayList<String>();
        description = new ArrayList<String>();  

        title_text = (TextView) findViewById(R.id.title_text);
        des_text = (TextView) findViewById(R.id.des_text);

        try {

            URL url = new URL(
                    "http://app2.nea.gov.sg/data/rss/nea_psi.xml");
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(new InputSource(url.openStream()));
            doc.getDocumentElement().normalize();

            NodeList nodeList = doc.getElementsByTagName("item");
            for (int i = 0; i < nodeList.getLength(); i++) {

                Node node = nodeList.item(i);       

                Element fstElmnt = (Element) node;
                NodeList nameList = fstElmnt.getElementsByTagName("title");
                Element nameElement = (Element) nameList.item(0);
                nameList = nameElement.getChildNodes();         

                title.add(""+ ((Node) nameList.item(0)).getNodeValue());

                NodeList websiteList = fstElmnt.getElementsByTagName("description");
                Element websiteElement = (Element) websiteList.item(0);
                websiteList = websiteElement.getChildNodes();

                description.add(""+ ((Node) websiteList.item(0)).getNodeValue());           

            }
        } catch (Exception e) {
            System.out.println("XML Pasing Excpetion = " + e);
        }


        title_text.setText(""+title.get(0));

        String temp = Html.fromHtml(description.get(0)).toString(); 
        String a[] = temp.split("\\)");
        des_text.setText(""+a[0]+")");
    }

mainpage_listitem_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
     > 
            <TextView 
                android:id="@+id/title_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"     
                android:text="title"
                android:layout_margin="5dp"
                android:textSize="22dp"
                android:textColor="#FFFFFF"/>   

             <TextView 
                android:id="@+id/des_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"     
                android:gravity="center"
                android:text="description "
                android:layout_margin="5dp"
                android:textSize="18dp"
                android:textColor="#FFFFFF"/>   
</LinearLayout>

这篇关于如何检索RSS源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆