Как загрузить локальный HTML-файл в Jsoup?

Я не могу загрузить локальный html файл, используя библиотеку Jsoup. Или, по крайней мере, кажется, что он его не признает. Я жестко закодировал точный html в локальном файле (как var 'html'), и когда я переключаюсь на это вместо ввода файла, код работает отлично. Но файл читается в обоих случаях.

import java.io.File;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;


public class FileHtmlParser{

public String input;


//constructor
public FileHtmlParser(String inputFile){input = inputFile;}


//methods
public FileHtmlParser execute(){

    File file = new File(input);
    System.out.println("The file can be read: " + file.canRead());

    String html = "<html><head><title>First parse</title><meta>106</meta> <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /></head>"
              + "<body><p>Parsed HTML into a doc.</p>" +
              "" +
              "<div id=\"navbar\">this is the div</div></body></html>";
            Document doc = Jsoup.parseBodyFragment(input);




    Elements content = doc.getElementsByTag("div");
    if(content.hasText()){System.out.println("result is " + content.outerHtml());}
    else System.out.println("nothing!");


    return this;
}

}/*endOfClass*/

Результат, когда:
Document doc = Jsoup.parseBodyFragment(html)

The file can be read: true
result is <div id="navbar">
this is the div
</div>

Результат, когда:
Документ документа = Jsoup.parseBodyFragment (вход)

The file can be read: true
nothing!
7
задан MeMory LEAk99 8 March 2012 в 01:05
поделиться