2012-12-28 8 views
5

Je suis en train d'analyser le tag HTML dans iOS en utilisant Hpple. je suis en mesure d'analyser les données où la balise HTML estAnalyse HTML <Tag> dans ios

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

En utilisant le code ios:

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

mais dans quelques cas, je n'ai pas portée, Imean la chaîne en HTML est accessible par tag "p" directement comme:

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

J'utilise ici le code ios comme:

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

mais ici j'obtiens une donnée vide en réponse.

quelqu'un peut-il me dire comment résoudre le problème?

Répondre

2

Comme parfois l'étiquette para a portée et parfois il ne pas, je suggère d'essayer de gérer cela en boucle sur les enfants

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); 
     } 
    } 
0
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"image" ofType:@"html" inDirectory:@"New Folder 2"]; 
NSData * data  = [NSData dataWithContentsOfFile:filePath]; 

NSFileHandle *readHandle = [NSFileHandle fileHandleForReadingAtPath:filePath]; 

NSString *htmlString = [[NSString alloc] initWithData:[readHandle readDataToEndOfFile] encoding:NSUTF8StringEncoding]; 


TFHpple * Parser  = [[TFHpple alloc] initWithHTMLData:data]; 

NSString *query = @"//p"; 

NSArray *nodes = [Parser searchWithXPathQuery:query]; 
for (TFHppleElement *item in nodes) 
    { 
     NSLog(@"Title : %@", item.content); 

      NSLog(@"URL : %@", [item.attributes valueForKey:@"href"]); 
     }