I have a method that parses RSS from differents url's and works great:
For example: https://www.clarin.com/rss/lo-ultimo/
But in one of these url (https://www.cio.com/category/mobile/index.rss) and in all of the RSS of that web, when I execute the code, the console shows me the following error and the parser doesn't works:
org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Premature end of file.
I'am parsing the RSS feed's with this method (a part of the code):
try {
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
URL url = new URL("https://www.cio.com/category/mobile/index.rss");
URLConnection urlConnection = url.openConnection();
InputStream inputStream = urlConnection.getInputStream();
Document doc = dBuilder.parse(inputStream);
The error happens in the last line -> Document doc = dBuilder.parse(inputStream);
In that code I'am parsing the RSS from the url, the strange thing is that when I parse the RSS directly from the file (index.rss) I have no errors and the parsing works great, I do this using:
File fXmlFile = new File("index.rss");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
Document doc = dBuilder.parse(inputStream);
doc.getDocumentElement().normalize();
To notice:
- This is a maven webapp project.
- Deployed in Tomcat 9.0 server.
- The method run when I press a button in the web's main page.
I mention that because when I tried in a simple java project, the parser works fine with the inputStream too.
I would appreciate very much if you could help me with this, thanks!