【问题标题】:html table td contents parsing using jsoup in android在android中使用jsoup解析html table td内容
【发布时间】:2015-02-05 10:21:03
【问题描述】:

我有一些 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”内容。

任何帮助将不胜感激。

【问题讨论】:

    标签: java android jsoup


    【解决方案1】:

    下面的源代码中清楚地描述了一切。

    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 内容由@​​987654323@ 分隔。如果需要,请使用String.split() 获取每个内容。

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

    【讨论】:

    • 你到底想要什么TD内容?在途中? 2015 年 1 月 31 日星期六晚上 7:09?还是德里?我的回答会给你所有这些。
    • 你不能完全复制我的代码。我创建了一个示例 HTML 文件来测试您上面列出的 HTML 代码。对于您的情况,您将不得不使用 Jsoup 来解析 URL。读一读。它并不难。 jsoup.org/cookbook/extracting-data/dom-navigation
    猜你喜欢
    • 1970-01-01
    • 2013-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-21
    • 1970-01-01
    相关资源
    最近更新 更多