问题描述:

I get the following error (at the "for (Element div : divs)" part):

Type mismatch: cannot convert from element type org.jsoup.nodes.Element to android.sax.Element

With the following code:

 Document doc = Jsoup.connect("www.example.com").get();

Elements divs = doc.select("div#heading");

for (Element div : divs)

{

System.out.println(div.text());

}

and the following imports:

import org.jsoup.Jsoup;

import org.jsoup.nodes.Document;

import org.jsoup.select.Elements;

import android.app.Activity;

import android.os.Bundle;

import android.sax.Element;

import android.view.Menu;

How can I fix this>

网友答案:

android.sax.Element is the wrong import --> org.jsoup.nodes.Element

Edit

Here's my test of your code (no exceptionhandling done here!):

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class Main
{
    public static void main(String[] args) throws Exception
    {
        Document doc = Jsoup.connect("http://www.tip.it/runescape").get();
        Elements divs = doc.select("div[class=heading]");

        for( Element div : divs )
        {
            System.out.println(div.text());
        }
    }
}
相关阅读:
Top