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

标签 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);
        }

这工作正常,没有任何问题,并显示在 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/

相关文章:

java - 如何将 YAML 文件解析为 Java 类

android - PagerTabStrip不显示标题,滑动时会显示时间

Android tabcontent 包含将内容隐藏在选项卡后面的 ScrollView

html - 为什么我的标题强制水平滚动条?

html - CSS Overflow 似乎没有被兑现

java - 用于创建、解析和查询 XML 的最佳 Java 库

blackberry - 解析黑莓 .ipd 文件

android - 使用 Superpowered 循环播放器时出现延迟/咔哒声

android - 从基于安卓的移动条码扫描仪检测条码扫描事件

javascript - 使用 href onclick 更新 div 而无需重新加载页面?