在 Android 中解析 HTML [英] Parse HTML in Android

查看:32
本文介绍了在 Android 中解析 HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从网页解析 android 中的 HTML,由于网页格式不正确,我收到 SAXException.

I am trying to parse HTML in android from a webpage, and since the webpage it not well formed, I get SAXException.

有没有办法在 Android 中解析 HTML?

Is there a way to parse HTML in Android?

推荐答案

我刚遇到这个问题.我尝试了一些方法,但最终决定使用 JSoup.jar大概132k,有点大,但是如果你下载源码,把一些你不会用到的方法拿出来,那就没那么大了.
=> 好处是它可以处理格式错误的 HTML

I just encountered this problem. I tried a few things, but settled on using JSoup. The jar is about 132k, which is a bit big, but if you download the source and take out some of the methods you will not be using, then it is not as big.
=> Good thing about it is that it will handle badly formed HTML

这是他们网站上的一个很好的例子.

Here's a good example from their site.

File input = new File("/tmp/input.html");
Document doc = Jsoup.parse(input, "UTF-8", "http://example.com/");

//http://jsoup.org/cookbook/input/load-document-from-url
//Document doc = Jsoup.connect("http://example.com/").get();

Element content = doc.getElementById("content");
Elements links = content.getElementsByTag("a");
for (Element link : links) {
  String linkHref = link.attr("href");
  String linkText = link.text();
}

这篇关于在 Android 中解析 HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆