WebEngine.getDocument
lợi nhuận org.w3c.dom.Document
, không hoạt Javascript tài liệu mà bạn mong đợi xét xử của mã của bạn.
Thật không may, in ra org.w3c.dom.Document
yêu cầu khá nhiều mã hóa. Bạn có thể thử giải pháp từ What is the shortest way to pretty print a org.w3c.dom.Document to stdout?, xem mã bên dưới.
Lưu ý rằng bạn cần đợi cho đến khi tài liệu được tải trước khi làm việc với Document
. Đây là lý do tại sao LoadWorker
được sử dụng ở đây:
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();
}
Nguồn
2013-01-11 18:13:08
phương pháp của bạn sẽ lấy nội dung thiếu nếu trang web có lỗi html ví dụ: Tôi rất quan tâm đến chủ đề này nếu bạn có một sửa chữa xin vui lòng giúp –