Results 1 to 7 of 7
  1. #1
    stephanehk is offline Member
    Join Date
    Sep 2016
    Posts
    3
    Rep Power
    0

    Default Jsoup error casting node.element to element.Element

    I am trying to iterate through two Elements in java using Jsoup, however, I am getting a runtime exception error. It appears that I cannot cast the type of nodes.Element to element.Element.

    I would really appreciate your help, thanks.

    Code:

    Java Code:
     Elements elements1 =  doc.select("title"); 
        Elements elements2 =  doc.select("link"); 
    
        Iterator<org.jsoup.nodes.Element> iter1 = elements1.iterator();
        Iterator<org.jsoup.nodes.Element> iter2 = elements2.iterator();
    
            while (iter1.hasNext() && iter2.hasNext()) {
                Element element1 = (Element) iter1.next();
                System.out.print(element1);
                Element element2 = (Element) iter2.next();
                System.out.print(element2);
    
            }
    Runtime Error:

    Java Code:
    java.lang.ClassCastException: org.jsoup.nodes.Element cannot be cast to javax.lang.model.element.Element
    Imports:
    Java Code:
    import java.util.Iterator;
    
    import javax.lang.model.element.Element;
    
    import org.jsoup.Jsoup;
    import org.jsoup.select.Elements;

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Jsoup error casting node.element to element.Element

    Apparently there is more than one class with the name of Element. You are probably using the wrong one. Try correcting the
    imports or cast with the full package/class name.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    stephanehk is offline Member
    Join Date
    Sep 2016
    Posts
    3
    Rep Power
    0

    Default Re: Jsoup error casting node.element to element.Element

    Quote Originally Posted by jim829 View Post
    Apparently there is more than one class with the name of Element. You are probably using the wrong one. Try correcting the
    imports or cast with the full package/class name.

    Regards,
    Jim
    Sorry for the nube question, but how might I cast with the full package/class name. Thanks.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Jsoup error casting node.element to element.Element

    Actually, a cast isn't the best solution. If you are using an IDE which does automatic
    imports them remove the import and try to import the correct one (or enter it manually).
    Try the following:

    Java Code:
    import org.jsoup.nodes.Element; // type this in by hand at the top of the file
    
    Elements elements1 =  doc.select("title"); 
       Elements elements2 =  doc.select("link"); 
     
    // now the following code should compile and run
       Iterator<Element> iter1 = elements1.iterator();
       Iterator<Element> iter2 = elements2.iterator();
    
     
           while (iter1.hasNext() && iter2.hasNext()) {
               Element element1 = (Element) iter1.next();
               System.out.print(element1);
               Element element2 = (Element) iter2.next();
               System.out.print(element2);
           }

    If this doesn't work, then post the error message provided.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Jsoup error casting node.element to element.Element

    The fact that it's using the full class name in those Iterator declarations implies exactly what jim has said. You have imported the wrong Element class at the top of your file. Probably a simple case of having picked the first one in the list the IDE gave.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Jsoup error casting node.element to element.Element

    Quote Originally Posted by stephanehk View Post
    Runtime Error:

    Java Code:
    java.lang.ClassCastException: org.jsoup.nodes.Element cannot be cast to javax.lang.model.element.Element
    The Exception message tells it all; you should've read it: the real type of the element is org.jsoup.nodes.Element; you are trying to cast it to a javax.lang.model.Element. That doesn't work of course because those two types have nothing in common ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Jsoup error casting node.element to element.Element

    One more thing I thought about after posting this. I forgot to remove the right hand casts from lines 12 and 14
    in my previous post (thus the hazards of cut and paste). If you properly declare your Iterator and target variables,
    casting is not required.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Adding new xml element on an existing xml element in JAVA
    By blertadhimitri in forum New To Java
    Replies: 0
    Last Post: 01-13-2014, 05:55 PM
  2. error for empty element in sax parser
    By alexandra12 in forum XML
    Replies: 9
    Last Post: 08-01-2012, 12:59 AM
  3. Replies: 5
    Last Post: 12-21-2011, 10:45 PM
  4. How to get a node value of an XML element?
    By rsenth99 in forum Java Servlet
    Replies: 9
    Last Post: 02-15-2010, 11:35 AM
  5. Get selected Node Value of a child element
    By Ms.Ranjan in forum XML
    Replies: 6
    Last Post: 10-06-2009, 09:31 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •