java.lang.IllegalArgumentException:不支持的元素:rss [英] java.lang.IllegalArgumentException: Unsupported element: rss

查看:44
本文介绍了java.lang.IllegalArgumentException:不支持的元素:rss的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取"RSS 提要.

I am trying to 'GET' a rss feed.

 public RssFeed(String url) {
    _url = url;
    String res = this.api.get(url);
    ByteArrayInputStream bis = new ByteArrayInputStream(res.getBytes());

    try {
        bis.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    XMLDecoder decoder = new XMLDecoder(bis);
    try {
        Object xml = decoder.readObject();
        _response = xml.toString();
    } catch(Exception e) {
        e.printStackTrace();
    } finally {
        decoder.close();
    }
}

当我检查res"中的内容时.它似乎得到了整个 XML.但是,我试图解码它,我得到:

When I check what's inside of 'res'. It appears to get this entire XML. But then, I am trying to decode it and I get:

java.lang.IllegalArgumentException:不支持的元素:rss

有人可以帮我吗?我是 Java 新手.

Can someone help me with that? I am new to Java.

谢谢!

推荐答案

XMLDecoder 旨在用于由 XMLEncoder 创建的元素.由于您是从 Web 上抓取此 XML,因此根据这些类,此 XML 中的元素可能无效.使用更通用的 XML 解析器,例如 DocumentBuilder::parse() 来处理这个.

XMLDecoder is meant to be used on elements created by XMLEncoder. Since you're scraping this XML from the web, the elements in this XML may not be valid according to these classes. Use a more generic XML parser, such as DocumentBuilder::parse() to handle this.

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();

try {
    builder.parse(url);
} catch (IOException e) {
    e.printStackTrace();
} catch (SAXParseException e) {
    e.printStackTrace();
} catch (IllegalArgumentException e) {
    e.printStackTrace();
}

这篇关于java.lang.IllegalArgumentException:不支持的元素:rss的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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