如何在android中使用Jsoup解析HTML标签

时间:2014-05-13 07:13:11

标签: android html parsing android-activity jsoup

我正在开发一个Android应用程序,我正在使用Android中的Jsoup从网站解析html内容。

<meta name="title" content="Notices for the week - Holy Family Church, Pestom Sagar" />

为此,我写道:

 @Override
        protected Void doInBackground(Void... params) {
            try {
                // Connect to the web site
                org.jsoup.nodes.Document document = Jsoup.connect(url).get();
                // Get the html document title   
                title=document.select("meta[name=title]");
                desc = title.attr("content");

            } catch (IOException e) {
                e.printStackTrace();
            } catch(NullPointerException ex){
                System.out.println(ex);
            }
            return null; 
        }
@Override
        protected void onPostExecute(Void result)
        {
            // Set title into TextView
            t1.setText(desc);
        }

这没有任何问题,并且在textView of Activity中显示正常。现在我想从该网站解析h3标签。

<h3 xmlns="http://www.w3.org/1999/xhtml" id="sites-page-title-header" style="" align="left">
<span id="sites-page-title" dir="ltr">Notices for the week</span>
</h3>

我不知道如何做到这一点,并在android活动中使用TextView显示它。请建议我...如果我想解析整个div标签并使用textView将其显示为活动.. !!!

1 个答案:

答案 0 :(得分:2)

您可以直接选择h3标签:

String h3=document. select("h3").text();
textView.setText(h3);

或者

 textView. setText(document.select("span[id=sites-page-title]").first().text());