簡體   English   中英

在Android中使用jsoup解析html table td內容

[英]html table td contents parsing using jsoup in android

我已經有了一些html表內容。對於我的應用程序,我想使用android中的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>

從此表中,我需要“ 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;
}

您可以在文本視圖中操作String。 所有TD內容都由||分隔。 如果需要,使用String.split()獲取每個內容。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM