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
3 Answers
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;
}
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.
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!
}
}
URI
.