I want to extract data from the html page with the help of jsoup and xpath.
This is my java code :-
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.w3c.dom.NodeList;
public class RssFeedRead {
public static void main(String args[])
{
try
{
Document doc = Jsoup.connect("http://timesofindia.indiatimes.com/world/china/China-sees-red-in-Abes-WWII-shrine-visit/articleshow/27989418.cms").get();
String title = doc.title();
System.out.println(title);
String exp = "//*[@id='cmtMainBox']/div/div[@class='cmtBox']/div/div[@class='box']/div[@class='cmt']/div/span";
XPathFactory factory = XPathFactory.newInstance();
XPath xPath = factory.newXPath();
XPathExpression expr = xPath.compile(exp);
NodeList node = (NodeList) expr.evaluate(doc, XPathConstants.NODE);
for (int i = 0; i < node.getLength(); i++)
{
System.out.println(expr.evaluate(node.item(i), XPathConstants.STRING));
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
This error occurred :-
java.lang.ClassCastException: org.jsoup.nodes.Document cannot be cast to org.w3c.dom.Node
so help me to solve this error