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