【问题标题】:How to Parse Html tags From Website in Android如何在 Android 中从网站解析 Html 标签
【发布时间】:2013-01-05 07:05:36
【问题描述】:

我正在尝试从以下标签中获取文本 网址:http://www.mcpss.com/?PN='News2'&SubP='DNewsStory'&gn=&NewsID=47318&ShowNav=&StoryGroup=Current

<td class="header">

OPEN HOUSE SCHEDULED AT CLARK-SHAW

</td>

<p><span style="font-size: 12pt;">January 16, 2013 - Due to the relocation of Murphy High School to the Clark-Shaw campus and the necessary construction that is still ongoing, Clark-Shaw school did not participate in the magnet school &ldquo;See and Sign&rdquo; January 11 and 12<sup>th</sup>. We would like to resume giving school tours and meeting interested parents. Therefore, we are planning an &ldquo;Open House&rdquo; on Friday, January 25 from 9:00 a.m.- 12:00 p.m. to coincide with our school&rsquo;s Science Fair Open House that is scheduled for that day.</span></p>
<p><span style="font-size: 12pt;">&nbsp;</span></p>
<p><span style="font-size: 12pt;">Please share this information with your friends and neighbors.&nbsp;&nbsp; Magnet School applications are available now online.</span></p>
<p>&nbsp;</p>

我想在 android 应用程序中表示文本,如下所示:

*CLARK-SHAW 开放日安排

2013 年 1 月 16 日 - 由于墨菲高中搬迁至克拉克肖校区以及仍在进行中的必要建设,克拉克肖学校没有参加 1 月 11 日和12 日。我们想恢复学校参观和会见感兴趣的家长。因此,我们计划在 1 月 25 日星期五上午 9:00 至下午 12:00 举行“开放日”。以配合当天安排的我们学校的科学博览会开放日。

请与您的朋友和邻居分享此信息。 Magnet School 申请现已在线提供。*

我如何在 android 中实现它。

【问题讨论】:

  • 其实我的疑惑是如何选择一个特定的

    标签我不知道如何给出查询路径

  • 你为什么不尝试在webview加载一个页面。

标签: android html-parsing


【解决方案1】:

使用 jsoup http://jsoup.org/ 你可以得到这个

下载 jsoup.jar 文件然后将其添加到您的 libs 文件夹然后转到 android 依赖项右键单击 >> 构建路径 >> 配置构建路径 >> 添加 JARS >> 库 >> 然后选择您下载的 jsoup.jar 文件

 try {
      String website="http://www.mcpss.com/?PN='News2'&SubP='DNewsStory'&gn=&NewsID=47318&ShowNav=&StoryGroup=Current";
      Document doc = Jsoup.connect(website).get();
      Elements el=doc.getElementsByClass("header");
      String text=el.text();
    } catch (Exception e) {
        Log.wtf("name of activity","error message to show in log", e);
    }

【讨论】:

  • 我如何从上面的以下 p 标签中解析特定的

    标签

  • doc.getElementsByTag("nameoftag");在你为你正在寻找的特定

    标签获取元素循环之后......毫无疑问,该函数会拾取许多其他

    标签

  • 在上面的 url 中有很多

    2013 年 1 月 16 日 - 由于墨菲高中搬迁到克拉克肖校园和仍在进行中的必要建设,Clark-Shaw 学校没有参与磁石学校的“看和签”。 1 月 11 日至 12 日th。我们想继续进行学校参观和会见感兴趣的家长。

    标签 我如何识别特定的

    标签。

  • 不确定是否有任何特别的方法,因为这些标签没有任何标识符......如果您可以访问该网站,您可以添加 id 以按照您的方式与标签一起提供应用程序可以选择它们....如果您无权访问,恐怕您可能只需要遍历标签过滤掉您不想要的标签
  • 请注意,如果您要使用我告诉您的方法(循环通过标签),您可能希望在服务器上执行此操作,然后让该服务器将结果发送到您的应用程序....因为这是一个成本核算过程,会导致应用程序出现一些延迟
【解决方案2】:

由于这是 Html 文档,请尝试将 Html.fromHtml(string) 用于 TextView 或尝试使用 webview 取决于标签。

对于 TextView,您可以像这样使用它

TextView txt=new TextView(getApplicationContext());
        String str="<td class=\"header\">"+
        "OPEN HOUSE SCHEDULED AT CLARK-SHAW"+
        "</td>"+
        "<p><span style=\"font-size: 12pt;\">January 16, 2013 - Due to the relocation of Murphy High School to the Clark-Shaw campus and the necessary construction that is still ongoing, Clark-Shaw school did not participate in the magnet school &ldquo;See and Sign&rdquo; January 11 and 12<sup>th</sup>. We would like to resume giving school tours and meeting interested parents. Therefore, we are planning an &ldquo;Open House&rdquo; on Friday, January 25 from 9:00 a.m.- 12:00 p.m. to coincide with our school&rsquo;s Science Fair Open House that is scheduled for that day.</span></p>"+
        "<p><span style=\"font-size: 12pt;\">&nbsp;</span></p>"+
        "<p><span style=\"font-size: 12pt;\">Please share this information with your friends and neighbors.&nbsp;&nbsp; Magnet School applications are available now online.</span></p>"+
        "<p>&nbsp;</p>";

        txt.setText(Html.fromHtml(str));

对于 WebView 尝试使用 webview.loadData();

【讨论】:

    猜你喜欢
    相关资源
    最近更新 更多
    热门标签