Hpple with complex HTML in iOS

576 Views Asked by At

How I can parse complex html with many child tags with hpple in iOS. I cant find any good and complex tutorial or any good documentation about this library. with simple parsing there is no any problem. the html example is:

<div class="post shortcuts_item focus" id="post_243273">
<div class="published">today in 21:03</div>
<h1 class="title">
    <a href="http://example.ru/post/243273/" class="post_title">Some simple title text here</a>
</h1>

<div class="hubs">
    <a href="http://example.ru/hub/internet_regulation/" class="hub">Simple text 1</a><span class="profiled_hub" title="Profile 1">*</span>, 
    <a href="http://example.ru/hub/business-laws/" class="hub">Simple text 2</a><span class="profiled_hub" title="Profile 2">*</span>, 
    <a href="http://example.ru/hub/vkontakte/" class="hub">Simple text 3</a><span class="profiled_hub" title="Profile 3">*</span>, 
    <a href="http://example.ru/hub/social_networks/" class="hub">Simple text 4</a><span class="profiled_hub" title="Profile 4">*</span>, 
    <a href="http://example.ru/hub/facebook/" class="hub">Simple text 5</a><span class="profiled_hub" title="Profile 5">*</span>
</div>
<div class="content html_format">
    <img src="//example.org/files/aa9/f6c/8a0/aa9f6c8a049e405c9f72bf10ee3a2e9f.png"><br>
    <br>
    Simple but very long text with link tag  <a href="http://example.ru/post/243273/#blahblah">Simple link...</a>
    <div class="buttons">
        <a href="http://example.ru/post/243273/#habracut">Read more...</a>
    </div>
    <div class="clear"></div>   
</div>
</div>

The question is how to get all A tags inside the class="hubs" with one run. How I can do that?

1

There are 1 best solutions below

0
On BEST ANSWER

You can use the // search syntax to search anywhere in the HTML, but then use the @class='xxx' syntax to search for tags of a particular class. Thus, you can search for either //a[@class='hub'] or //div[@class='hubs']/a:

TFHpple *parser = [TFHpple hppleWithHTMLData:data];
NSArray *nodes = [parser searchWithXPathQuery:@"//a[@class='hub']"];
for (TFHppleElement *element in nodes) {
    NSString *href = [element attributes][@"href"];
    NSString *content = [element content];
    NSLog(@"%@ -> %@", href, content);
}

See How to Parse HTML on iOS on Ray Wenderlich's site.