Java GUI显示网页并返回HTML

Java GUI显示网页并返回HTML,第1张

Java GUI显示网页并返回HTML

这是一个使用JavaFX的人为示例,该示例将html内容打印到System.out
-适应创建

getHtml()
方法应该不会太复杂。(我已经用JavaFX 8对其进行了测试,但是它也应该可以与JavaFX 2一起使用)。

每次加载新页面时,该代码都会打印HTML内容。

注意:我已

printdocument
从此答案中借用了代码。

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<Worker.State>() {     @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);    }}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5475835.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-12
下一篇 2022-12-12

发表评论

登录后才能评论

评论列表(0条)

保存