如何从Android中的SAX xml Parser获取属性值?

时间:2011-09-27 09:04:13

标签: android xml parsing sax

我的XML是:

<?xml version='1.0' encoding='UTF-8'?>
<entry xmlns='http://www.w3.org/2005/Atom' xmlns:media='http://search.yahoo.com/mrss/'
    xmlns:gd='http://schemas.google.com/g/2005' xmlns:yt='http://gdata.youtube.com/schemas/2007'>

    <media:group>

        <media:content
            url='http://www.youtube.com/v/UBCNwgFlw1A?f=videos&amp;app=youtube_gdata'
            type='application/x-shockwave-flash' medium='video' isDefault='true'
            expression='full' duration='217' yt:format='5' />
        <media:content
            url='rtsp://v4.cache7.c.youtube.com/CiILENy73wIaGQlQw2UBwo0QUBMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp'
            type='video/3gpp' medium='video' expression='full' duration='217'
            yt:format='1' />
        <media:content
            url='rtsp://v2.cache6.c.youtube.com/CiILENy73wIaGQlQw2UBwo0QUBMYESARFEgGUgZ2aWRlb3MM/0/0/0/video.3gp'
            type='video/3gpp' medium='video' expression='full' duration='217'
            yt:format='6' />

    </media:group>

</entry>

.java文件

public void startElement(String arg0, String localName, String arg2,
        Attributes attributes) throws SAXException {
    currentElement = true;

if (localName.equals("content")) {

        String videourl = attributes.getValue("url");
        Log.v("log_tag", "Video url is : " + videourl);
        message.setVideourl(videourl);


        String duration = attributes.getValue("duration");
        Log.v("log_tag", "Duration of Video is : " + duration);


        String formaturl1 = attributes.getValue("format");

        Log.v("log_tag", "format of url is : " + formaturl);

    }


   }

我使用了SAX解析器。我可以获得所有Atrribute媒体:内容。但我不能得到yt:格式值,所以我怎么能得到yt:Android中的格式值。

2 个答案:

答案 0 :(得分:0)

看起来attributes.getValue("yt:format")attributes.getValue("http://gdata.youtube.com/schemas/2007", "format")都应该有效。

答案 1 :(得分:-1)

你可以使用attributes.getValue(5);