java 显示网页,Java GUI中显示网页和HTML返回

I need a workflow like below:

// load xyz.com in the browser window

// the browser is live, meaning users can interact with it

browser.load("http://www.google.com");

// return the HTML of the initially loaded page

String page = browser.getHTML();

// after some time

// user might have navigated to a new page, get HTML again

String newpage = browser.getHTML();

I am surprised to see how hard this is to do with Java GUIs such as JavaFX (http://lexandera.com/2009/01/extracting-html-from-a-webview/) and Swing.

Is there some simple way to get this functionality in Java?

解决方案

Here is a contrived example using JavaFX that prints the html content to System.out - it should not be too complicated to adapt to create a getHtml() method. (I have tested it with JavaFX 8 but it should work with JavaFX 2 too).

The code will print the HTML content everytime a new page is loaded.

Note: I have borrowed the printDocument code from this answer.

public class TestFX extends Application {

@Override

public void start(Stage stage) throws Exception {

try {

final WebView webView = new WebView();

final WebEngine webEngine = webView.getEngine();

Scene scene = new Scene(webView);

stage.setScene(scene);

stage.setWidth(1200);

stage.setHeight(600);

stage.show();

webEngine.getLoadWorker().stateProperty().addListener(new ChangeListener() {

@Override

public void changed(ObservableValue extends State> ov, State t, State t1) {

if (t1 == Worker.State.SUCCEEDED) {

try {

printDocument(webEngine.getDocument(), System.out);

} catch (Exception e) { e.printStackTrace(); }

}

}

});

webView.getEngine().load("http://www.google.com");

} catch (Exception e) {

e.printStackTrace();

}

}

public static void printDocument(Document doc, OutputStream out) throws IOException, TransformerException {

TransformerFactory tf = TransformerFactory.newInstance();

Transformer transformer = tf.newTransformer();

transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");

transformer.setOutputProperty(OutputKeys.METHOD, "xml");

transformer.setOutputProperty(OutputKeys.INDENT, "yes");

transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");

transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

transformer.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(out, "UTF-8")));

}

public static void main(String[] args) {

launch(args);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值