user4925913 user4925913 - 8 months ago 29
Javascript Question

Get content source from webview?

I have a server that when posted to will display XML like this :

<?xml version="1.0"?>

In my code for the Webviewer I want to get this from the response in the Webview.

The only way I have found to get content from a webview is to inject javascript and get it.

This is what I have done so far :

webViewLogin.setWebViewClient(new WebViewClient() {

boolean flag = false;

public void onPageStarted(WebView view, final String url, Bitmap favicon) {

if (url.contains("success.php")) {
flag = true;


public void onPageFinished(WebView view, String url) {

if (flag) {
webViewLogin.evaluateJavascript("(function() { return ('<html>'+document.getElementsByTagName('html')[0].innerHTML+'</html>'); })();",
new ValueCallback<String>() {
public void onReceiveValue(String html) {
Log.d("HTML", html);





However when I look at my returned HTML I am getting :

\u003Chtml>\u003Chead xmlns=\"\">\u003Cstyle id=\"xml-viewer-style\">/* Copyright 2014 The Chromium Authors. All rights reserved.\n * Use of this source code is governed by a BSD-style license that can be\n * found in the LICENSE file.\n */\n\ndiv.header {\n border-bottom: 2px solid black;\n padding-bottom: 5px;\n margin: 10px;\n}\n\ndiv.collapsible &gt; div.hidden {\n display:none;\n}\n\n.pretty-print {\n margin-top: 1em;\n margin-left: 20px;\n font-family: monospace;\n font-size: 13px;\n}\n\n#webkit-xml-viewer-source-xml {\n display: none;\n}\n\n.collapsible-content {\n margin-left: 1em;\n}\n.comment {\n white-space: pre;\n}\n\n.button {\n -webkit-user-select: none;\n cursor: pointer;\n display: inline-block;\n margin-left: -10px;\n width: 10px;\n background-repeat: no-repeat;\n background-position: left top;\n vertical-align: bottom;\n}\n\n.collapse-button {\n background: url(\"data:image/svg+xml,&lt;svg xmlns='' fill='#909090' width='10' height='10'&gt;&lt;path d='M0 0 L8 0 L4 7 Z'/&gt;&lt;/svg&gt;\");\n height: 10px;\n}\n\n.expand-button {\n background: url(\"data:image/svg+xml,&lt;svg xmlns='' fill='#909090' width='10' height='10'&gt;&lt;path d='M0 0 L0 8 L7 4 Z'/&gt;&lt;/svg&gt;\");\n height: 10px;\n}\n\u003C/style>\u003C/head>\u003Cbody xmlns=\"\">\u003Cdiv id=\"webkit-xml-viewer-source-xml\">\u003Cmessage>\n\t\u003Cresponse>1\u003C/response>\n\t\u003CresponseMessage>SUCCESS\u003C/responseMessage>\n\t\u003Cuser>\n\t\t\u003Cid>##\u003C/id>\n\t\t\u003CemailAddress>\u003C/emailAddress>\n\t\t\u003CfullName>TEST\u003C/fullName>\n\t\t\u003CcellPhone>+555555\u003C/cellPhone>\n

this technically has the XML inside but with all the extra styling used in chrome to easily display XML. Is there a way to just get the source and none of chromes automatic styling?

Answer Source

First I would decode / get rid of all the extra characters. Here is a function that can do that:

String unslashUnicode(String slashed) {

    ArrayList<String> pieces = new ArrayList<>();
    String temp = "";

    while (true) {

        if (slashed.contains("\\u")) {

            pieces.add(slashed.substring(0, slashed.indexOf("\\u")));
            char c = (char) Integer.parseInt(slashed.substring(slashed.indexOf("\\u") + 2, slashed.indexOf("\\u") + 6), 16);
            slashed = slashed.substring(slashed.indexOf("\\u") + 6, slashed.length());

            pieces.add(c + "");

        } else 


    for (String s : pieces)
        temp = temp + s;

    slashed = temp + slashed;
    return slashed;


Since you are grabbing the xml from a webviewer which stylizes it. There is actually a tag that simply contains the xml. a div with the id of webkit-xml-viewer-source-xml

So change your webViewLogin.evaluateJavascript to have this string instead:

(function() { return (document.getElementById('webkit-xml-viewer-source-xml').innerHTML); })();

From inside

public void onReceiveValue(String html) {

You can then get the straight html like so:

html = "<?xml version=\"1.0\"?>" + unslashUnicode(html).replaceAll("\"", "");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download