WebEngine.getDocument
返回org.w3c.dom.Document
,而不是您期望通过代码判断的 JavaScript 文档。
不幸的是,打印出来org.w3c.dom.Document
需要相当多的编码。您可以尝试从What is the shortest way to pretty print a org.w3c.dom.Document to stdout? ,请参见下面的代码。
请注意,在使用Document
. 这就是LoadWorker
这里使用的原因:
public void start(Stage primaryStage) {
WebView webview = new WebView();
final WebEngine webengine = webview.getEngine();
webengine.getLoadWorker().stateProperty().addListener(
new ChangeListener<State>() {
public void changed(ObservableValue ov, State oldState, State newState) {
if (newState == Worker.State.SUCCEEDED) {
Document doc = webengine.getDocument();
try {
Transformer transformer = TransformerFactory.newInstance().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(System.out, "UTF-8")));
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
});
webengine.load("http://stackoverflow.com");
primaryStage.setScene(new Scene(webview, 800, 800));
primaryStage.show();
}