开发者

android: parse html from page

i would like to parse out some text from a page.

Is there an easy way to save the product info in to a string for example? Example url: http://upcdata.info/upc/73108开发者_开发百科70008741

Thanks


Jsoup is excellent at parsing simple HTML from Android applications:

http://jsoup.org/

To get the page, just do this:

URL url = new URL("http://upcdata.info/upc/7310870008741");
Document document = Jsoup.parse(url, 5000);

Then you can parse out whatever you need from the Document. Check out this link for a brief description of how to extract parts of the page:

http://jsoup.org/cookbook/extracting-data/dom-navigation


If you want to read from a URL into a String:

StringBuffer myString = new StringBuffer();
try {
    String thisLine;
    URL u = new URL("http://www.google.com");
    DataInputStream theHTML = new DataInputStream(u.openStream());
    while ((thisLine = theHTML.readLine()) != null) {
        myString.append(thisLine);
    } 
} catch (MalformedURLException e) {

} catch (IOException e) {

}

// call toString() on myString to get the contents of the file your URL is
// pointing to.

This will give you a plain old string, HTML markup and all.


String tmpHtml = "<html>a whole bunch of html stuff</html>"; 
String htmlTextStr = Html.fromHtml(tmpHtml).toString(); 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜