Using Document Object Model

The Document Object Model (DOM) can be retrieved through WebEngine.getDocument(), which returns an org.w3c.dom.Document giving access to the whole web page model.

In the following demonstration, we will find and print all links on a web page using this API:

// chapter9/web/DOMModelDemo.javapublic void start(Stage stage) {    WebView webView = new WebView();    WebEngine webEngine = webView.getEngine();    webEngine.load("https://stackoverflow.com/questions/tagged/javafx");    webEngine.getLoadWorker().stateProperty().addListener(            (observable, oldValue, newValue) -> {                // remember, we need to get the page loaded first                if (newValue == Worker.State.SUCCEEDED) {                    NodeList links = webEngine.getDocument().getElementsByTagName("a"); ...

Get Mastering JavaFX 10 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.