Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing HTML <Tag> into ios

i am Parsing HTML Tag into iOS using Hpple. i am able to parse the data where the HTML Tag is

 <div id="NewsPageSubTitle">
         <p><**span** hi how are you>

Using ios code:

NSString *tutorialsXpathQueryString = @"//div[@id='NewsPageArticle']/p/span ";
 NSArray *tutorialsNodes = [tutorialsParser searchWithXPathQuery:tutorialsXpathQueryString];

but in few case i don't have span, imean the string in html is accessed by tag "p" directly like:

<div id="NewsPageSubTitle">
             <p>< hi how are you>

Here I am using ios code as:

NSString *tutorialsXpathQueryString = @"//div[@id='NewsPageArticle']/p ";
     NSArray *tutorialsNodes = [tutorialsParser searchWithXPathQuery:tutorialsXpathQueryString];

but here i am getting a blank data in response.

can any one let me know how to solve the problem?

like image 204
Pradeep Kumar Avatar asked Oct 05 '22 12:10

Pradeep Kumar


2 Answers

Since sometimes the para tag has span and sometimes it does not, I would suggest trying to handle that by looping over the children

    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
    NSData  * data      = [NSData dataWithContentsOfFile:filePath];
    TFHpple * tutorialsParser       = [[TFHpple alloc] initWithHTMLData:data];

    NSString *tutorialsXpathQueryString = @"//div[@id='NewsPageSubTitle']";
    NSArray *tutorialsNodes = [tutorialsParser searchWithXPathQuery:tutorialsXpathQueryString];

    for (TFHppleElement * element in tutorialsNodes) {
        NSLog(@"%@", element);
        NSLog(@"%@", [element tagName]);
        NSLog(@"%@", [element attributes]);
        NSLog(@"%@", [element children]);
        for (TFHppleElement *childElement in [element children]) {
                NSLog(@"%@", childElement);
        }
    }
like image 143
Vineet Bhatia Avatar answered Oct 08 '22 02:10

Vineet Bhatia


Check with this: https://github.com/mwaterfall/MWFeedParser

This will provide the HTML Parser for iphone sdk.

More help on:

this blog and here.

like image 42
A J Avatar answered Oct 08 '22 03:10

A J