0

TFHpple(XPathを使用)を使用してHTMLドキュメントを解析しています。さまざまなノードのコンテンツなどを取得できますが、GitHubで見つけたコードは不完全なようです。ノードの属性を取得するメソッドがありますが、子配列があります。だから私はそれを書いて失敗しました。以下の2つの「属性」メソッドは正常に機能します。基本的に、子配列を新しいノードとして取得したいと思います。私はNSDictionaryレベルで失敗していると思います。以下の「子」で返すものから新しいTFHppleElementを作成しようとしましたが、失敗しました。手がかりはありますか?

これは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];
}
4

2 に答える 2

0

私はそれを十分にやっていたと思います。しかし、私は最終的に子ノード配列を次のように単純化しました。

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

ダウンロードしたTFHppleElement.mコードにこれを追加しました。次に、その結​​果を取得して、必要な属性やコンテンツを抽出できる新しいノードを作成できます。

TFHppleElement *parentNode;
.
.
.
TFHppleElement *childNode = [[[TFHppleElement alloc] initWithNode: [parentNode children]] autorelease];
于 2010-09-08T02:41:42.880 に答える
0

TFHppleElementインターフェース/実装に以下を追加します。

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

次に、子供が必要な場合:

...
FHppleElement *rowAtIndex = [xpathParser at:[NSString stringWithFormat:oddRowAtIndex,ii,x]];
...
...
TFHppleElement *children = [[TFHppleElement alloc] initWithNode:[rowAtIndex children]];
...
于 2011-04-11T17:42:17.100 に答える