Android的XML与RSS解析(Android XML Parsing with RSS)

2019-08-22 04:17发布

我有我的RSS文件的项目:

<item >
<title>Prasad</title>
<link>http://www.tele.com/rssHostDescr.php?hostId=15</link>
<guid isPermaLink="false">http://www.tele.com/rssHostDescr.php?hostId=14</guid>
<pubDate>2013-04-10</pubDate>
<description>Prasad</description>
<media:thumbnail width='66' height='49' url='http://www.tele.com/hos/Panthi-image.jpg'></media:thumbnail>
</item>
<item>
........................
</item>

etc.....................

我试图分析上面的文件,我能得到的所有信息(标题,链接,日期),但我的要求是获得url属性值。 我如何得到媒体的URL值:缩略图标签? 可以在任何一个帮助吗?

这里是我的代码:

public class AndroidXMLParsingActivity extends ListActivity {

    // All static variables
    static final String URL = "http://www.tele.com/RSSFeed/toriHos.php";
    // XML node keys
    static final String KEY_ITEM = "item"; // parent node
    static final String KEY_TITLE=  "title";

    static final String KEY_LINK = "link";
    static final String KEY_PUBDATE = "pubDate";
    static final String KEY_MEDIA = "media:thumbnail";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();

        XMLParser parser = new XMLParser();
        String xml = parser.getXmlFromUrl(URL); // getting XML
        Document doc = parser.getDomElement(xml); // getting DOM element

        NodeList nl = doc.getElementsByTagName(KEY_ITEM);
        NodeList n=doc.getElementsByTagName(KEY_MEDIA);

        // looping through all item nodes <item>
        for (int i = 0; i < nl.getLength(); i++) {
            // creating new HashMap
            HashMap<String, String> map = new HashMap<String, String>();
            Element e = (Element) nl.item(i);
            // adding each child node to HashMap key => value
            map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
            map.put(KEY_LINK, parser.getValue(e, KEY_LINK));
            map.put(KEY_PUBDATE, parser.getValue(e, KEY_PUBDATE));
            map.put(KEY_MEDIA, parser.getValue(e, KEY_MEDIA));//Here I'm not getting any value

            // adding HashList to ArrayList
            menuItems.add(map);
        }
    }
}

注:map.put(KEY_MEDIA,parser.getValue(即KEY_MEDIA)); //这里,我没有得到任何价值

Answer 1:

Element e = (Element) nl.item(i);
String urlStr = e.getAttribute("url");

希望这会帮助你。



文章来源: Android XML Parsing with RSS