html table td 内容在 android 中使用 jsoup 解析

html table td contents parsing using jsoup in android

我有一些 html table contents.And 用于我的应用程序我想在 android.But 中使用 JSOUP 解析来解析这些 html 内容我我是这个 JSOUP 方法的新手,我无法正确解析那些 html 内容。

HTML数据:

<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>

从这个table我需要"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;
}

您可以在文本视图中操作字符串。所有的TD内容都用||分隔。如果需要,请使用 String.split() 获取每个内容。

String[] data = tdContents.split("\|\|");