Search code examples
javaxpathjsoup

Error : java.lang.ClassCastException: org.jsoup.nodes.Document cannot be cast to org.w3c.dom.Node


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


Solution

  • Please highlight the line where the exception was thrown and don't omit the stack trace.

    This is the problematic line:

    NodeList node = (NodeList) expr.evaluate(doc, XPathConstants.NODE);
    

    You are mixing two APIs for document parsing and handling, XPath and JSoup. An XPath expression does not know about JSoup documents and can't handle them.

    You need to decide which of both APIs you want to use for your specific job.