Problem:
Because the Android WebView and other related classes do not provide an interface to parse the HTML Web page content, we want to get the content of the Web page and parse out the content of the elements we want, with Android's inherent API is no way.
Here I provide two kinds of parsing HTML ideas: The first, using third-party parsing HTML Library , the second, the use of Java and JS callback, through JS parsing HTML.
Previously, we studied the open source Library of Htmlparser, but found that there was a conflict with the libraries provided by Android, but it was feasible under pure Java applications.
htmlparser:http://htmlparser.sourceforge.net/
Then continue to search, whether there is more suitable for Android HTML parsing library, Kung Fu, or find a better open source library, this is jsoup.
Jsoup is a Java HTML parser that can parse a URL address and HTML text content directly. It provides a very labor-saving API that can be used to extract and manipulate data through dom,css and jquery-like operations. This version contains a parser branch that supports HTML5, which ensures that the HTML is parsed like the current browser, while reducing parsing time and memory usage.
You can use a DOM-like method to parse a Web page, for example, I have downloaded a Web page to the local, save as input.html, then through the following code, we can get the ID of the content of the DOM, all the hyperlinks. The string linkhref gets the link address of the link, and the string linktext gets the text description of the link.
jsoup:http://jsoup.org/
Jsoup GitHub Address: Https://github.com/scrylove/jsoup
Jsoup Getting Started:http://blog.csdn.net/zjx409/article/details/23375565
File input = new file ("/tmp/input.html"); Document doc = jsoup.parse (input, "UTF-8", "http://example.com/"); Element content = Doc.getelementbyid ("content"); Elements links = Content.getelementsbytag ("a"); for (Element link:links) { String linkhref = link.attr ("href"); String LinkText = Link.text (); } file Input = new file ("/tmp/input.html");D ocument doc = jsoup.parse (input, "UTF-8", "http://example.com/"); Element content = Doc.getelementbyid ("content"); Elements links = Content.getelementsbytag ("a"); for (Element link:links) { String linkhref = link.attr ("href"); String LinkText = Link.text ();}
There are many other ways to parse HTML in a variety of different situations. Here is not listed, there may be students can do their own research.
As for the second way of thinking , here is no longer wordy, Java and JS Intermodulation should everyone, as for the JS parsing HTML that will be given to the Web front-end developers, we Android client, just need to get their resolution completed, callback to our results on the line.
Lesson reference this article: http://veikr.com/201106/android_webview_content-html.html
How to parse a webpage in Android to get the content of an element in a webpage