我正在开发一个 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);
}
这工作正常,没有任何问题,并显示在 Activity 的 textView 中。现在我想从该网站解析 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 Activity 中使用 TextView 执行此操作和显示此操作。请建议我......另外,如果我想解析整个 div 标签并使用 textView 将其显示到 Activity 中......!!!
最佳答案
您可以直接选择 h3 标签:
String h3=document. select("h3").text();
textView.setText(h3);
或者
textView. setText(document.select("span[id=sites-page-title]").first().text());
关于android - 如何在android中使用Jsoup解析HTML标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23624854/