Android的XML的RSS解析Android、XML、RSS

2023-09-04 04:07:52 作者:感情都是走私货

我有我的RSS文件的项目:

 <项目>
<冠军>普拉萨德< /标题>
<链接> HTTP://www.tele.com/rssHostDescr.php HOSTID = 15℃; /链接>
&所述; GUID isPermaLink =假>的http://www.tele.com/rssHostDescr.php HOSTID = 14抗体或其/ guid的>
< pubdate的> 2013年4月10日< / pubdate的>
<描述>普拉萨德< /描述>
<媒体:缩略图宽度= '66'高度= '49'URL =HTTP://www.tele.com/hos/Panthi-image.jpg'>< /媒体:缩略图>
< /项目>
<项目>
........................
< /项目>

等等.....................
 

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

下面是我的code:

 公共类AndroidXMLParsingActivity扩展ListActivity {

    //所有静态变量
    静态最终字符串URL =htt​​p://www.tele.com/RSSFeed/toriHos.php;
    // XML节点键
    静态最后弦乐KEY_ITEM =项目; //父节点
    静态最后弦乐KEY_TITLE =称号;

    静态最后弦乐KEY_LINK =链接;
    静态最后弦乐KEY_PUBDATE =pubdate的;
    静态最后弦乐KEY_MEDIA =媒体:缩略图;

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);

        ArrayList的< HashMap的<字符串,字符串>>菜单项=新的ArrayList< HashMap的<字符串,字符串>>();

        XMLParser的分析器=新XMLParser的();
        XML字符串= parser.getXmlFromUrl(URL); //获取XML
        文档DOC = parser.getDomElement(XML); //获取DOM元素

        NodeList的NL = doc.getElementsByTagName(KEY_ITEM);
        节点列表N = doc.getElementsByTagName(KEY_MEDIA);

        //遍历所有的项目节点<项目>
        的for(int i = 0; I< nl.getLength();我++){
            //创建新的HashMap
            HashMap的<字符串,字符串>图=新的HashMap<字符串,字符串>();
            元素e =(元)nl.item(我);
            //添加每个子节点HashMap中的key =>值
            map.put(KEY_TITLE,parser.getValue(即KEY_TITLE));
            map.put(KEY_LINK,parser.getValue(即KEY_LINK));
            map.put(KEY_PUBDATE,parser.getValue(即KEY_PUBDATE));
            map.put(KEY_MEDIA,parser.getValue(即KEY_MEDIA)); //在这里我没有得到任何价值

            //添加HashList到ArrayList中
            menuItems.add(图)
        }
    }
}
 
Android中如何解析Xml

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

解决方案

 元素E =(元)nl.item(我);
字符串urlStr = e.getAttribute(URL);
 

希望这会帮助你。

I have My Rss File Items :

<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.....................

I'm trying to parse the above file,I'm able to get all the information(title,link,date)but my requirement is to get url attribute value. How do I get the URL value from media:thumbnail tag? Could any one help?

Here's my code:

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

Note:map.put(KEY_MEDIA, parser.getValue(e, KEY_MEDIA));//Here I'm not getting any value

解决方案

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

Hope this will help you.