views:

27

answers:

1

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];
}
A: 

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];
Troy Sartain