2

RSS の解析に TouchXML を使用しています。 新しい XML

 <channel>
 <item> 
 <title> </title>
  <social>
   <views>
    <total_views>2</total_views>
   </views>
   <reviews>
    <average_rating>2</average_rating>
   </reviews>
  </social>
 </item>
 </channel>

initWithData現在、XML を解析して、記事のタイトルの詳細ビューに次のように渡しています。

 [rssData objectForKey: @"title" ];

しかし、星の値がレートのサブノードであり、レートがソーシャルのサブノードであることをどのように示すのですか?

私はこれを試しました:[rssData objectForKey: @"social/rate/stars" ];

しかし、それはうまくいきません。方法を教えてください。

4

2 に答える 2

1

私が元の回答に基づいたという前提全体が間違っているようです。次のスニペットを使用した場合、

for (CXMLElement *node in nodes) {
    NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
    int counter;
    for(counter = 0; counter < [node childCount]; counter++) {
        //  common procedure: dictionary with keys/values from XML node
        [item setObject:[[node childAtIndex:counter] stringValue] forKey:[[node childAtIndex:counter] name]];
    }

    [results addObject:item];
    [item release];
}    

social要素の子が文字列値として格納され、それらを抽出するための合理的な手段が失われるよりも、コードを生成する必要があります。ディクショナリを作成するのではなく、ノードを保存することをお勧めします。評価を抽出する例を次に示します。

NSString * xmlString = @"<channel> <item> <title> </title> <social> <views> <total_views>2</total_views> </views> <reviews> <average_rating>2</average_rating> </reviews> </social> </item> </channel>";

CXMLDocument * xmlDocument = [[[CXMLDocument alloc] initWithXMLString:xmlString options:0 error:nil] autorelease];
NSArray * nodes = [xmlDocument nodesForXPath:@"//item" error:nil];

for (CXMLElement * node in nodes) {

    NSString * title = [[node nodeForXPath:@"title" error:nil] stringValue];
    NSString * average_rating = [[node nodeForXPath:@"social/reviews/average_rating" error:nil] stringValue] ;
    NSLog(@"%@", title);
    NSLog(@"%@", average_rating);
}    
于 2011-06-22T00:13:06.917 に答える
0

これを試してみてください。「channle」のような readNode 値を渡す必要があり、子サブノードを含む配列が返されます

 -(NSMutableArray *)parseNodeFromXML:(NSString *)strXML readForNode:(NSString *)strReadValue
    {
        NSError *theError = NULL;
        CXMLDocument *theXMLDocument = [[CXMLDocument alloc] initWithXMLString:strXML options:0 error:&theError] ;

        NSMutableArray *arrReturn = [[NSMutableArray alloc] init];
        NSArray *nodes = [[theXMLDocument rootElement] nodesForXPath:strReadValue error:nil];

        for (CXMLNode *itemNode in nodes)
        {

            NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];

            // PROCESS FOR READ ELEMENT ATTRIBUTES IN CURRENT NODE -------------------------

            if([itemNode isMemberOfClass:[CXMLElement class]]){
                CXMLElement *elements=(CXMLElement*)itemNode;
                NSArray *arAttr=[elements attributes];

                for (int i=0; i<[arAttr count]; i++) {
                    if([[arAttr objectAtIndex:i] name] && [[arAttr objectAtIndex:i] stringValue]){
                        [dic setValue:[[arAttr objectAtIndex:i] stringValue] forKey:[[arAttr objectAtIndex:i] name]];
                    }
                }
            }


            // PROCESS FOR READ CHILD NODE IN CURRENT NODE -------------------------

            for (CXMLNode *childNode in [itemNode children])
            {
                //   NSLog(@"count -- %d ---  %@",[childNode childCount],[childNode children]);

                // IF ANY CHILD NODE HAVE MULTIPLE CHILDRENS THEN DO THIS....- 
                if ([childNode childCount] > 1)
                {
                    NSMutableDictionary *dicChild = [[NSMutableDictionary alloc] init];
                    for (CXMLNode *ChildItemNode in [childNode children])
                    {
                        [dicChild setValue:[ChildItemNode stringValue] forKey:[ChildItemNode name]];
                    }
                     [dic setValue:dicChild forKey:[childNode name]];
                }else
                {
                    [dic setValue:[childNode stringValue] forKey:[childNode name]];
                }

            }

            [arrReturn addObject:dic];
        }

        //    NSLog(@"%@",arrReturn);
        return arrReturn;
    }
于 2013-06-13T12:44:12.680 に答える