开发者

How to parse HTML Heading

I have this HTML i am parsing.

<div id="articleHeader">
<h1 class="headline">Assassin's Creed Revelations: The Three Heroes</h1>
<h2 class="subheadline">Exclusive videos and art spanning three eras of assassins.</h2>
<h2 class="publish-date"><script>showUSloc=(checkLocale('uk')||checkLocale('au'));document.writeln(开发者_C百科showUSloc ? '<strong>US, </strong>' : '');</script>

<span class="us_details">September 22, 2011</span>

What i want to do it parse the "headline" subheadline and publish date all to seperate Strings


Just use the proper CSS selectors to grab them.

Document document = Jsoup.connect(url).get();
String headline = document.select("#articleHeader .headline").text();
String subheadline = document.select("#articleHeader .subheadline").text();
String us_details = document.select("#articleHeader .us_details").text();
// ...

Or a tad more efficient:

Document document = Jsoup.connect(url).get();
Element articleHeader = document.select("#articleHeader").first();
String headline = articleHeader.select(".headline").text();
String subheadline = articleHeader.select(".subheadline").text();
String us_details = articleHeader.select(".us_details").text();
// ...


Android has a SAX parser built into it . You can use other standard XML parsers as well.

But I think if ur HTML is simple enough u could use RegEx to extract string.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜