简体   繁体   English

解析中 <table> 来自html的代码,然后在webview中显示

[英]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. 我的方法是:连接到站点,将html放入字符串中,将表数据放入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. 我已经成功连接到网站,将HTML放入名为myString的变量中,甚至已经将myString加载到Web视图中作为测试。

My question is what is the best way to only load the <table> everything here </table> . 我的问题是什么是仅将<table>此处所有内容加载</table>的最佳方法。 In other words, I only want the table data. 换句话说,我只想要表数据。 I have experimented with xpath but can't seem to get it to work. 我已经尝试过xpath,但似乎无法使其正常工作。 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. 然后将myString加载到您的WebView中,它应该只包含原始网页中的第一个表。


Edit: The above example is case-sensitive, ie it would not find an upper-case <TABLE> tag. 编辑:上面的示例区分大小写,即找不到大写的<TABLE>标记。 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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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