Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
434 views
in Technique[技术] by (71.8m points)

How can you parse HTML in android?

I am making an application for android, and an element of the functionality of the application is to return results from an online search of a library's catalogue. The application needs to display the results of the search, which is carried out by way of a custom HTML form, in a manner in keeping with the rest of the application. Ie, the results of the search need to be parsed and the useful elements displayed. I was just wondering if/how this could be achieved in android?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You would use a Html Parser. One that i use and works VERY well is JSoup This is where you will need to begin with parsing html. Also Apache Jericho is another good one.

You would retrieve the html document by using DOM, and use the JSOUP Select() method to select any tags that you would like to get. Either by tag, id, or class.

Solution

Use the: Jsoup.connect(String url) method:

 Document doc = Jsoup.connect("http://example.com/").get();

This will allow you to connect to the html page by using the url. And store it as the Document doc, Through DOM. And the read from it using the selector() method.

Description

The connect(String url) method creates a new Connection, and get() fetches and parses a HTML file. If an error occurs whilst fetching the URL, it will throw an IOException, which you should handle appropriately.

The Connection interface is designed for method chaining to build specific requests:

 Document doc = Jsoup.connect("http://example.com")

If you read through the documentation on Jsoup you should be able to achieve this.

EDIT: Here is how you would use the selector method

  //Once the Document is retrieved above, use these selector methods to Extract the   data you want by using the tags, id, or css class 

  Elements links = doc.select("a[href]"); // a with href
  Elements pngs = doc.select("img[src$=.png]");
  // img with src ending .png

  Element masthead = doc.select("div.masthead").first();
  // div with class=masthead

  Elements resultLinks = doc.select("h3.r > a"); // direct a after h3

EDIT: Using JSOUP you could use this to get attributes, text,

Document doc = Jsoup.connect("http://example.com")
Element link = doc.select("a").first();

String text = doc.body().text(); // "An example link"
String linkHref = link.attr("href"); // "http://example.com/"
String linkText = link.text(); // "example""

String linkOuterH = link.outerHtml(); 
// "<a href="http://example.com"><b>example</b></a>"
String linkInnerH = link.html(); // "<b>example</b>"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...