简体   繁体   中英

Parsing <table> tag from html, then display in webview

I need to take a table from a website and put it into an app. My methodology is this: connect to a site, place html into a string, place table data into a webview. I have successfully connected to the website, placed the HTML into a variable called myString and have even the loaded myString into a webview as a test.

My question is what is the best way to only load the <table> everything here </table> . In other words, I only want the table data. I have experimented with xpath but can't seem to get it to work. Also, looking ahead to the next step I will ultimately have to change the padding and whatnot of the table to make it look better on the device correct?

If I understand you correctly, something like this might serve your purposes:

int start = myString.indexOf("<table");
if( start < 0 )
    Log.d(this.toString(), "Table start tag not found");
else {
    int end = myString.indexOf("</table>", start) + 8;
    if( end < 0 )
        Log.d(this.toString(), "Table end tag not found");
    else
        myString = "<html><body>" + myString.substring(start, end) + "</body></html>";
}

Then load myString into your WebView, and it should contain only the first table from the original web page.


Edit: The above example is case-sensitive, ie it would not find an upper-case <TABLE> tag. Though it may not be the most efficient method, one way to make it case-insensitive would be to convert the string to lower-case before searching it, eg:

int start = myString.toLowerCase().indexOf("<table");
...
int end = myString.toLowerCase().indexOf("</table>", start) + 8;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM