1

I need to check RSS feed URL validation. I have a url, now I need to check this url is only for RSS feed, how do I check that in core java? please help

2
  • You have no choice but to access that URL to check if it is actually an RSS feed... As to checking its shape, use URI.
    – fge
    Commented Jul 8, 2013 at 10:48
  • yes, I think that is only the way
    – RITUser
    Commented Jul 8, 2013 at 10:54

3 Answers 3

1

I know it was 3 years ago), but here is my code. Use the library of rome. 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;
}
0

I did it in a simple way, don't know how far it meets, but in my case it is helpful. Bellow is my code snippet

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

that's it.

0

The best way is to use XML Validation for that: Download XSD file from here and put it behind you class file: http://europa.eu/rapid/conf/RSS20.xsd

You can use XSD with 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!
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.