3

ページのulを使用してhtmlファイルをダウンロードしようとしています。Jsoupを使用しています。これは私のコードです:

TextView ptext = (TextView) findViewById(R.id.pagetext);
    Document doc = null;
    try {
         doc = (Document) Jsoup.connect(mNewLinkUrl).get();
    } catch (IOException e) {
        Log.d(TAG, e.toString());
        e.printStackTrace();
    }
    NodeList nl = doc.getElementsByTagName("meta");
    Element meta = (Element) nl.item(0); 
    String title = meta.attr("title"); 
    ptext.append("\n" + mNewLinkUrl);

実行すると、type要素にattrが定義されていないというエラーが表示されます。私は何を間違えましたか?これが些細なことだと思われる場合はご容赦ください。

4

2 に答える 2

2

Elementそれが他のものではなく、を参照していることを確認してくださいorg.jsoup.nodes.Element。インポートを確認します。また、それがをDocument参照していることを確認してorg.jsoup.nodes.Documentください。つまり、方法がありませんgetElementsByTagName()。Jsoupはorg.w3c.domAPIを利用していません。

正しいインポートを使用した完全な例を次に示します。

package com.stackoverflow.q4720189;

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

public class Test {

    public static void main(String[] args) throws Exception {
        Document document = Jsoup.connect("http://example.com").get();
        Element firstMeta = document.select("meta").first();
        String title = firstMeta.attr("title"); 
        // ...
    }

}
于 2011-01-18T03:21:56.100 に答える
0

私が理解しているように、ここの要素はですorg.w3c.dom.Element。次に、meta.getAttribute()の代わりに使用しattrます。要素クラスには、そのようなメソッドがありません。

于 2011-01-11T07:02:07.130 に答える