Java 在android中使用jsoup解析html表td内容
我随身携带了一些html表内容。对于我的应用程序,我想在android中使用JSOUP解析来解析这些html内容。但我对这个JSOUP方法不熟悉,无法正确解析这些html内容 HTML数据:Java 在android中使用jsoup解析html表td内容,java,android,jsoup,Java,Android,Jsoup,我随身携带了一些html表内容。对于我的应用程序,我想在android中使用JSOUP解析来解析这些html内容。但我对这个JSOUP方法不熟悉,无法正确解析这些html内容 HTML数据: <table id="box-table-a" summary="Tracking Result"> <thead> <tr> <th width="20%">AWB / Ref. No.</th>
<table id="box-table-a" summary="Tracking Result">
<thead>
<tr>
<th width="20%">AWB / Ref. No.</th>
<th width="30%">Status</th>
<th width="30%">Date Time</th>
<th width="20%">Location</th>
</tr>
</thead>
<tbody>
<tr>
<td width="20%" nowrap="nowrap" class="click"><a href="Javascript:void(0);" onclick="Javascript: document.frm_Z45681583.submit();">Z45681583</a></td>
<td width="30%" nowrap="nowrap" class="click">
IN TRANSIT<div id='ntfylink' style='display:block; text-decoration:blink'><a href='#' class='topopup' name='modal' style='text-decoration:none'><font face='Verdana' color='#DF0000'><blink>Notify Me</blink></font></a></div>
</td>
<td width="30%">
Sat, Jan, 31, 2015 07:09 PM
</td>
<td width="20%">DELHI</td>
</tr>
</tbody>
</table>
AWB/参考号。
地位
日期时间
地方
在途中
2015年1月31日星期六晚上7:09
德里
从这个表中,我需要“td”内容
任何帮助都将不胜感激。下面的源代码中清楚地描述了所有内容
private static String test(String htmlFile) {
File input = null;
Document doc = null;
Elements tdEles = null;
Element table = null;
String tdContents = "";
try {
input = new File(htmlFile);
doc = Jsoup.parse(input, "ASCII", "");
doc.outputSettings().charset("ASCII");
doc.outputSettings().escapeMode(EscapeMode.base);
/** Get table with id = box-table-a **/
table = doc.getElementById("box-table-a");
if (table != null) {
/** Get td tag elements **/
tdEles = table.getElementsByTag("td");
/** Loop each of the td element and get the content by ownText() **/
if (tdEles != null && tdEles.size() > 0) {
for (Element e: tdEles) {
String ownText = e.ownText();
//Delimiter as "||"
if (ownText != null && ownText.length() > 0)
tdContents += ownText + "||";
}
if (tdContents.length() > 0) {
tdContents = tdContents.substring(0, tdContents.length() - 2);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return tdContents;
}
您可以在textview中操纵字符串。所有TD内容由| |
分隔。如果需要,可以使用String.split()
获取每个内容
String[] data = tdContents.split("\\|\\|");
你到底想要什么TD内容?在途中?2015年1月31日星期六晚上7:09?还是德里?我的答案将返回所有这些。您不能完全复制我的代码。我创建了一个示例HTML文件来测试上面列出的HTML代码。对于您的情况,您必须使用Jsoup来解析URL。读一读。这并不难。