Java中的RSS源URL验证

时间:2013-07-08 10:43:45

标签: java validation url rss feed

我需要检查RSS提要URL验证。我有一个url,现在我需要检查这个url仅用于RSS feed,我如何检查核心java?请帮忙

3 个答案:

答案 0 :(得分:0)

我是以一种简单的方式做到的,不知道它遇到了多远,但在我的情况下,这是有帮助的。贝娄是我的代码片段

DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(url); // url is your url for testing
doc.getDocumentElement().getNodeName().equalsIgnoreCase("rss")

就是这样。

答案 1 :(得分:0)

最好的方法是使用XML验证: 从这里下载XSD文件并将其放在您的类文件后面: http://europa.eu/rapid/conf/RSS20.xsd

您可以将XSD与DOM一起使用:


private void validate(final File file) throws SAXException, ParserConfigurationException, IOException {
    final List exceptions = new ArrayList();

    final ErrorHandler errorHandler = new ErrorHandler() {
        public void warning(SAXParseException e) throws SAXException {
            // we can forgive that!
        }

        public void error(SAXParseException e) throws SAXException {
            exceptions.add(e);
        }

        public void fatalError(SAXParseException e) throws SAXException {
            exceptions.add(e);
        }
    };

    final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(false);
    factory.setNamespaceAware(true);

    final SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
    final Schema schema = schemaFactory.newSchema(getClass().getClassLoader().getResource("Rss20.xsd"));

    factory.setSchema(schema);

    final DocumentBuilder builder = factory.newDocumentBuilder();

    builder.setErrorHandler(errorHandler);

    builder.parse(file);

    if(exceptions.size() == 0) {
        // no error
    } else {
       // Error happens!
    }
}

答案 2 :(得分:0)

我知道这是3年前的事了),但这是我的代码。 使用罗马的图书馆。 Using ROME to read a syndication feed

public boolean romeLibraryExample(String address) {
    boolean ok = false;
    try{
        URL url = new URL(address);
        HttpURLConnection httpcon = (HttpURLConnection)url.openConnection();
        SyndFeedInput input = new SyndFeedInput();
        SyndFeed feed = input.build(new XmlReader(url));
        ok = true;
    } catch (Exception exc){
        exc.printStackTrace();
    }
    return ok;
}