سؤال

أنا أستخدم TFHpple (الذي يستخدم XPath) لتحليل مستند HTML. يمكنني الحصول على محتوى العقد المختلفة ، وما إلى ذلك ، لكن الرمز الذي وجدته على Github يبدو غير مكتمل. لديها طريقة للحصول على سمات العقدة ولكن صفيف الطفل. لذلك كتبت واحدة وفشلت. تعمل طريقتان "السمات" أدناه بشكل جيد. في الأساس ، أود الحصول على صفيف الطفل كعقدة جديدة. أعتقد أنني فشلت على مستوى 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];

نصائح أخرى

أضف ما يلي إلى واجهة/تطبيق TFHPPleEment الخاص بك:

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

ثم ، عندما تحتاج إلى الأطفال:

...
FHppleElement *rowAtIndex = [xpathParser at:[NSString stringWithFormat:oddRowAtIndex,ii,x]];
...
...
TFHppleElement *children = [[TFHppleElement alloc] initWithNode:[rowAtIndex children]];
...
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top