如何在Android中从网站解析Html标签

时间:2013-01-22 05:14:53

标签: android html-parsing

我正在尝试从下面的标签中获取文本 网址: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日 - 由于Murphy高中搬迁至Clark-Shaw校区并且仍在进行必要的施工,Clark-Shaw学校于1月11日没有参加磁铁学校“See and Sign” 12。我们希望恢复参加学校旅行和会见有兴趣的家长。因此,我们计划于1月25日星期五上午9点至中午12点开放“开放日”。与我们学校当天安排的科学博览会开放日同时进行。

请与您的朋友和邻居分享这些信息。磁铁学校的申请现已在线提供。*

我怎么能在android中实现它。

2 个答案:

答案 0 :(得分:1)

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

下载jsoup.jar文件,然后将其添加到您的libs文件夹,然后转到android dependancies右键单击&gt;&gt;构建路径&gt;&gt;配置构建路径&gt;&gt;添加JARS&gt;&gt; libs&gt;&gt;然后选择你下载的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);
    }

答案 1 :(得分:1)

由于这是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();