I have written a scheduler in java spring boot application which runs once in every hour, since month it was working completely fine. But today it has started throwing exception while parsing. I guess may be the xml(from which I am getting the data is broken or may be it has changed little bit which I am unable to figure out).
Please note: I cannot change the source data.
Here is my code:
@Scheduled(fixedRate = 1*60*60*1000 , initialDelay = 10*1000)
public String updateNewsFeed() {
try {
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
String URL = "https://nation.com.pk/rss/coronavirus";
Document doc = db.parse(URL);
List<NewsFeed> newsFeedList = parseNewsItemsToList(doc);
return "Works fine";
} catch (Exception ex) {
return ex.getMessage();
}
}
public List<NewsFeed> parseNewsItemsToList(Document doc) throws Exception{
doc.getDocumentElement().normalize();
NodeList nodes = doc.getElementsByTagName("item");
List<NewsFeed> newsFeedList = new ArrayList<>();
for (int i = 0; i < nodes.getLength(); i++) {
Element element = (Element) nodes.item(i);
NodeList title = element.getElementsByTagName("title");
NodeList link = element.getElementsByTagName("link");
NodeList description = element.getElementsByTagName("description");
NodeList pubDate = element.getElementsByTagName("pubDate");
NodeList guid = element.getElementsByTagName("guid");
org.jsoup.nodes.Document htmlDoc = Jsoup.connect(link.item(0).getTextContent().trim()).get();
/*Elements pngs = htmlDoc.select("picture");
System.out.println("\nimg link:"+pngs.toString());*/
String image = htmlDoc.select("picture").select("img[src~=(?i)\\.(png|jpe?g)]").attr("src").trim();
newsFeedList.add(new NewsFeed(
title.item(0).getTextContent().trim(),
description.item(0).getTextContent().trim(),
pubDate.item(0).getTextContent().trim(),
guid.item(0).getTextContent().trim(),
image,
link.item(0).getTextContent().trim()
));
}
return newsFeedList;
}
Here is the Error message:
[Fatal Error] coronavirus:195:32: The entity name must immediately follow the '&' in the entity reference. org.xml.sax.SAXParseException; systemId: https://nation.com.pk/rss/coronavirus; lineNumber: 195; columnNumber: 32; The entity name must immediately follow the '&' in the entity reference. at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:258) at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:339) at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:177) at com.i2p.covid19.service.NewsFeedService.updateNewsFeed(NewsFeedService.java:87) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:84) at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748)
Issue is the &
ampersand character in the XML.
<category>Lifestyle & Entertainment</category>
&
is an illegal in XML document outside CDATA
section. This must be written as &
but the producer of the XML document has already escaped the &
character.
If you replace the &
with &
, it will work.
Use ROMETOOLS library (https://rometools.github.io/rome/)
If your target is process the RSS feeds, I recommend to use rome
library which handles the special chars like &
- it is straightforward and simple to use. Refer https://www.baeldung.com/rome-rss
Below code snippet prints the International News
from the <title>
tag of the RSS feed:
URL feedSource = new URL("https://nation.com.pk/rss/coronavirus");
SyndFeedInput input = new SyndFeedInput();
SyndFeed feed = input.build(new XmlReader(feedSource));
System.out.println(feed.getTitle());