java.lang.IllegalArgumentException: Unsupported element: rss

天大地大妈咪最大 提交于 2019-12-11 06:08:59

问题


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

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: Unsupported element: rss

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

Thanks!


回答1:


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


来源:https://stackoverflow.com/questions/48711270/java-lang-illegalargumentexception-unsupported-element-rss

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!