Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hpple HTML Parsing Library Not Parsing Correctly on iOS 7

I am using the hpple parsing library in my iOS app. https://github.com/topfunky/hpple

I am having a problem where the parser is not correctly parsing my HTML page on iOS 7. It is correctly parsed on iOS 6 and I get the returned objects. On iOS 7 I am returned only a partial parse and a wrong one. The library incorrectly reads the HTML on iOS 7. I have tried debugging the problem and have concluded that the problem lies in this method.

NSArray *PerformXPathQuery(xmlDocPtr doc, NSString *query)
{
  xmlXPathContextPtr xpathCtx;
  xmlXPathObjectPtr xpathObj;

  /* Create xpath evaluation context */
  xpathCtx = xmlXPathNewContext(doc);
  if(xpathCtx == NULL)
    {
      //NSLog(@"Unable to create XPath context.");
      return nil;
    }

  /* Evaluate xpath expression */
  xpathObj = xmlXPathEvalExpression((xmlChar *)[query cStringUsingEncoding:NSUTF8StringEncoding], xpathCtx);
  if(xpathObj == NULL) {
    //NSLog(@"Unable to evaluate XPath.");
    xmlXPathFreeContext(xpathCtx);
    return nil;
  }

  xmlNodeSetPtr nodes = xpathObj->nodesetval;
  if (!nodes)
    {
      //NSLog(@"Nodes was nil.");
      xmlXPathFreeObject(xpathObj);
      xmlXPathFreeContext(xpathCtx);
      return nil;
    }

  NSMutableArray *resultNodes = [NSMutableArray array];
  for (NSInteger i = 0; i < nodes->nodeNr; i++)
    {
      NSDictionary *nodeDictionary = DictionaryForNode(nodes->nodeTab[i], nil,false);
      if (nodeDictionary)
        {
          [resultNodes addObject:nodeDictionary];
        }
    }

  /* Cleanup */
  xmlXPathFreeObject(xpathObj);
  xmlXPathFreeContext(xpathCtx);

  return resultNodes;
}

doc and query are not nil when this method is called. I don't know how to log the other classes in this method and I do not know which one of them returns the wrong parse. Maybe the problem lies in Apple's libxml2.dylib

I do not know exactly what the code does in the method until the end of the method. Any help will be greatly appreciated as I am stumped why the library cannot parse on iOS 7 the same HTML page it can parse on iOS 6.

On iOS 7 this library correctly parses other HTML pages in other apps. The code I use to parse initiate the parser is

 NSData *htmlData = [NSData dataWithContentsOfURL:dataURL];

    TFHpple *dataParser = [TFHpple hppleWithHTMLData:htmlData];

    NSString *dataXpathQueryString = @"//td[3]";
    NSArray *dataNodes = [dataParser searchWithXPathQuery:dataXpathQueryString];

    NSMutableArray *newData = [[NSMutableArray alloc] initWithCapacity:0];
    for (TFHppleElement *element in dataNodes) {

        data = [[GFCData alloc] init];
        [newData addObject:data];
        data.title = [[element firstChild] content];

        data.title = [data.title stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    }

where dataURL is http://www.gfchurch.com/ru/pages/video.php?a=5&b=51&c=37

I can provide more information if needed.

like image 353
maxned Avatar asked Nov 26 '22 08:11

maxned


1 Answers

This problem was solved by not using // in the searchPathQuery. I should test this problem again because the library was updated a month ago and maybe the issue was solved.

like image 84
maxned Avatar answered Dec 09 '22 12:12

maxned