TFHPPLEを使用して子供アレイの属性をどのように取得しますか?
-
30-09-2019 - |
質問
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];
}
解決
私はそれを十分にやっていたと思います。しかし、最終的には、子ノードアレイを次のようにすることを簡素化しました。
- (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];
他のヒント
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]];
...
所属していません StackOverflow