使用javafx从webview获取内容 [英] get the contents from the webview using javafx

查看:703
本文介绍了使用javafx从webview获取内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JAVA FX控件处理swing应用程序。在我的应用程序中,我必须打印出webview中显示的html页面。我正在尝试的是在HtmlDocuement的帮助下将webview的html内容加载到字符串中。

I am working on a swing application using JAVA FX controls . In my application i have to take print out the html page displayed in the webview . What I am trying is to load the html content of webview in a string with the help of HtmlDocuement.

要从Web视图加载html文件的内容,我正在使用以下代码但不起作用:

To load the content of html file from web view,I am using the following code but its not working:

try
{
    String str=webview1.getEngine().getDocment().Body().outerHtml();
}
catch(Exception ex)
{
}


推荐答案

WebEngine.getDocument 返回 org.w3c.dom.Document ,而不是您期望通过代码判断的JavaScript文档。

WebEngine.getDocument returns org.w3c.dom.Document, not JavaScript document which you expect judging by your code.

不幸的是,打印 org.w3c.dom.Document 需要相当多的编码。您可以尝试将org.w3c.dom.Document打印到stdout的最短方法是什么?,请参阅下面的代码。

Unfortunately, printing out org.w3c.dom.Document requires quite a bit of coding. You can try the solution from What is the shortest way to pretty print a org.w3c.dom.Document to stdout?, see code below.

请注意,您需要等到在使用 Document 之前加载文档。这就是为什么 LoadWorker 在这里使用的原因:

Note that you need to wait until the document is loaded before working with Document. This is why LoadWorker is used here:

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();
}

这篇关于使用javafx从webview获取内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆