0

我有我的 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 属性值,如何从 media:thumbnail 标记中获取 URL 值?有人可以帮忙吗?

这是我的代码:

public class HostsRssHandler extends DefaultHandler {
    private List<HostsProfile> messages;
    private HostsProfile currentMessage;
    private StringBuilder builder;

    public List<HostsProfile> getMessages(){
        return messages;
    }
    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {
        super.characters(ch, start, length);
        builder.append(ch, start, length);
    }
    @Override
    public void endElement(String uri, String localName, String name)
            throws SAXException {
        super.endElement(uri, localName, name);
        if (this.currentMessage != null){
            if (localName.equalsIgnoreCase("tilte")){
                currentMessage.setTitle(builder.toString());
            } else if (localName.equalsIgnoreCase("link")){
                currentMessage.setLink(builder.toString());
            }
                else if (localName.equalsIgnoreCase("media:thumbnail")){
                    currentMessage.setMediathumbnail(builder.toString());
                }
             else if (localName.equalsIgnoreCase("pubDate")){
                currentMessage.setDate(builder.toString());
            }


            else if (localName.equalsIgnoreCase("item")){
                messages.add(currentMessage);
            }
            builder.setLength(0);   
        }
    }

    @Override
    public void startDocument() throws SAXException {
        super.startDocument();
        messages = new ArrayList<HostsProfile>();
        builder = new StringBuilder();
    }

    @Override
    public void startElement(String uri, String localName, String name,
            Attributes attributes) throws SAXException {
        super.startElement(uri, localName, name, attributes);
        if (localName.equalsIgnoreCase("item")){
            this.currentMessage = new HostsProfile();
        }
    }   
}

笔记:

else if (localName.equalsIgnoreCase("media:thumbnail")){
                    currentMessage.setMediathumbnail(builder.toString());// During My Program Execution, Here I'm not able to get any value
                }
4

1 回答 1

1

您需要从属性中读取值,因此它必须位于startElement

@Override
public void startElement(String uri, String localName, String name,
        Attributes attributes) throws SAXException {
    super.startElement(uri, localName, name, attributes);
    if (localName.equalsIgnoreCase("item")){
        this.currentMessage = new HostsProfile();
    }
    //Validate that localName has the value you want. In theory localName shouldn't have the prefix
    if (localName.equalsIgnoreCase("thumbnail")) { 
      currentMessage.setMediaThumbnail(attributes.getValue("url"));
    }
}   
于 2013-04-12T04:43:29.817 回答