0

I have a WebView which is initialized with the following content:

<!DOCTYPE html><html>
<body>
<div class="section" id="header"></div>
<div class="section" id="content"></div>
<div class="section" id="status"></div>
</body>
</html>

I then get a reference to the "header" node, and attempt o set some HMTL to it:

org.w3c.dom.Document doc = webView.getEngine().getDocument();
Node headerNode = doc.getElementById("header");
headerNode.setNodeValue("<b>abc</b><i>defg</i>");

I don't, however, see any changes to the output in the webview. I then tried to read back the HTML from the webview, to check whether my update took place:

DOMSource domSource = new DOMSource(doc);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.transform(domSource, result);
System.out.println(writer.toString());

To my surprise, this was printed out:

<?xml version="1.0" encoding="UTF-8" standalone="no"?><HTML xmlns="http://www.w3.org/1999/xhtml"><BODY><DIV class="section" id="header"/><DIV class="section" id="content"/><DIV class="section" id="status"/></BODY></HTML>

Not sure why the doctype was changed, and why my update did not take effect. My question is:

How can I append that small HTML fragment to the DIV node I got a reference to?

Thanks!

Edy Bourne
  • 5,679
  • 13
  • 53
  • 101
  • you can make your dom modifications in JS, and call JS from java, see https://stackoverflow.com/questions/4325639/android-calling-javascript-functions-in-webview – njzk2 Apr 05 '18 at 02:32
  • @njxk2 Interesting... I will try this, thank you! – Edy Bourne Apr 05 '18 at 02:43
  • after your operations (`headerNode.setNodeValue("abcdefg");`) you can try to set the new HTML into WebView. I think that modifying node won't affect actual WebView page. – Vladyslav Matviienko Apr 05 '18 at 05:01
  • @VladyslavMatviienko the thing is, I'm attempting not to reset the whole HTML to prevent everything from being parsed, laid out and repainted. The hope is that by making a localized change only that section will be processed and repainted (I do a LOT of changes like those very very fast and won't want the CPU to be doing just that) – Edy Bourne Apr 05 '18 at 16:58
  • there are better ways to localize HTML I think. You can do it via JS. There are some JS libs already I think – Vladyslav Matviienko Apr 05 '18 at 17:37

0 Answers0