Question

I'm using TFHpple (which uses XPath) to parse an HTML document. I can get the content of various nodes, etc. But the code I found on GitHub seems incomplete. It has a method to get the attributes of a node but the child array. So I wrote one and failed. The two "attributes" methods below work fine. Basically, I'd like to get the child array as a new node. I think I'm failing at the NSDictionary level. I tried to make a new TFHppleElement out of what I return in "children" below but... FAIL! Any clues?

This is from TFHppleElement.m:

- (NSDictionary *) attributesForNode: (NSDictionary *) myNode
{
 NSMutableDictionary *translatedAttributes = [NSMutableDictionary dictionary];
 for (NSDictionary *attributeDict in [myNode objectForKey: TFHppleNodeAttributeArrayKey]) 
 {
  //NSLog(@"attributeDict: %@", attributeDict);
  [translatedAttributes setObject: [attributeDict objectForKey: TFHppleNodeContentKey]
         forKey: [attributeDict objectForKey: TFHppleNodeAttributeNameKey]];
 }
 return translatedAttributes;
}

- (NSDictionary *) attributes
{
 return [self attributesForNode: node];
}

- (BOOL) hasChildren
{
 return [node objectForKey: TFHppleNodeChildArrayKey] != nil;
}

- (NSDictionary *) children
{
 NSMutableDictionary *translatedChildren = [NSMutableDictionary dictionary];
 for (NSDictionary *childDict in [node objectForKey: TFHppleNodeChildArrayKey]) 
 {
  [translatedChildren setObject: childDict
          forKey: [childDict objectForKey: TFHppleNodeNameKey]];
 }
 return [node objectForKey: TFHppleNodeChildArrayKey];
}
Was it helpful?

Solution

I guess I was doing it right enough. But I eventually simplified getting the child node array as:

- (NSDictionary *) children
{
    if ([self hasChildren])
        return [[node objectForKey: TFHppleNodeChildArrayKey] objectAtIndex: 0];
    return nil;
}

I added this to the TFHppleElement.m code I had downloaded. Then I could take that result and create a new node from which I could extract any attributes or content needed.:

TFHppleElement *parentNode;
.
.
.
TFHppleElement *childNode = [[[TFHppleElement alloc] initWithNode: [parentNode children]] autorelease];

OTHER TIPS

Add the following to your TFHppleElement interface/implementation:

- (NSDictionary *)children
{
    return [[node objectForKey:@"nodeChildArray"] objectAtIndex:0];
}

Then, when you need the children:

...
FHppleElement *rowAtIndex = [xpathParser at:[NSString stringWithFormat:oddRowAtIndex,ii,x]];
...
...
TFHppleElement *children = [[TFHppleElement alloc] initWithNode:[rowAtIndex children]];
...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top