現在の iPhone アプリ プロジェクトの XML 要素をループする方法について、非常に混乱しています。コードを表示してから、質問を明確にします。
私が使用しているサンプルXMLは次のとおりです。
<restaurant_details>
<total_results>90</total_results>
<restaurant>
<name>Through the Garden</name>
<distance_from_current_location>0.55</distance_from_current_location>
<restaurant_id>123</restaurant_id>
<longitude>-84.373734</longitude>
<latitude>39.258606</latitude>
<address>10738 Kenwood Rd | cincinnati ,OH | 45242</address>
<phone_number>513-791-2199</phone_number>
<restaurant_type>General</restaurant_type>
</restaurant>
</restaurant_details>
テーブルのデータを取得する:
- (void)get_table_data {
NSString *day = [self dateInFormat:@"%A"];
NSLog (@"At get_table_data");
NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.url.com"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
NSLog (@"At connection");
receivedData = [NSMutableData data];
}
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[receivedData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[receivedData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
// NSString *theXML = [[NSString alloc] initWithBytes: [myWebData mutableBytes] length:[myWebData length] encoding:NSUTF8StringEncoding];
// NSLog(@"%@",theXML);[theXML release];
if(parser){
parser = nil;
}
parser = [[NSXMLParser alloc] initWithData: receivedData];
[parser setDelegate: self];
[parser setShouldResolveExternalEntities: YES];
[parser parse];
}
そして私のパーサーコード:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict{
NSLog (@"At parser");
currentElement = elementName;
if ([currentElement isEqualToString:@"restaurant_details"]) {
if ([currentElement isEqualToString:@"total_results"]) {
//NSLog(@"Element: %@", currentElement);
}else if ([currentElement isEqualToString:@"restaurant"]) {
restaurantObj = [[DOR_RestaurantClass alloc]init];
}
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
if([currentElement isEqualToString:@"name"]) {
restaurantObj.name=[NSString stringWithFormat:@"%@",string];
//NSLog(@"Name to be saved in Array :- %@",string);
}
if([currentElement isEqualToString:@"distance_from_current_location"]) {
restaurantObj.distance=[NSString stringWithFormat:@"%@",string];
//NSLog(@"Name to be saved in Array :- %@",string);
}
if([currentElement isEqualToString:@"restaurant_id"]) {
restaurantObj.restId=[NSString stringWithFormat:@"%@",string];
//NSLog(@"Name to be saved in Array :- %@",string);
}
if([currentElement isEqualToString:@"address"]) {
NSArray *stringArray = [string componentsSeparatedByString:@" | "];
restaurantObj.address=[stringArray objectAtIndex:0];
restaurantObj.address2=[stringArray objectAtIndex:1];
restaurantObj.address3=[stringArray objectAtIndex:2];
//NSLog(@"Name to be saved in Array :- %@",string);
}
if([currentElement isEqualToString:@"phone_number"]) {
restaurantObj.phone=[NSString stringWithFormat:@"%@",string];
//NSLog(@"Name to be saved in Array :- %@",string);
}
if([currentElement isEqualToString:@"description"]) {
restaurantObj.description=[NSString stringWithFormat:@"%@",string];
//NSLog(@"Name to be saved in Array :- %@",string);
}
if([currentElement isEqualToString:@"image_url"]) {
restaurantObj.image=[NSString stringWithFormat:@"%@",string];
//NSLog(@"Name to be saved in Array :- %@",string);
}
if([currentElement isEqualToString:@"restaurant_type"]) {
//restaurantObj.Name=[NSString stringWithFormat:@"%@",string];
//NSLog(@"Name to be saved in Array :- %@",string);
}
NSLog (@"At parser2");
if(!currentElementValue)
currentElementValue=[[NSMutableString alloc] initWithString:string];
else
[currentElementValue appendString:string];
NSLog(@"the parser just found this text in a tag:%@",string);
}
-(void)parser:(NSXMLParser*)parser didEndElement:(NSString*)elementName namespaceURI:(NSString*)namespaceURI qualifiedName:(NSString*)qualifiedName {
NSLog(@"Current element in End Element Category:- %@",currentElement);
if([elementName isEqualToString:@"restaurant"]) {
[listItems addObject:restaurantObj];
NSLog(@"Array: %@", listItems);
}else{
currentElementValue=nil;
}
}
私のレストランクラスの .h は次のとおりです。
#import <Foundation/Foundation.h>
@interface DOR_RestaurantClass : NSObject
@property (nonatomic, retain) NSString *restId;
@property (nonatomic, retain) NSString *image;
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *description;
@property (nonatomic, retain) NSString *distance;
@property (nonatomic, retain) NSString *address;
@property (nonatomic, retain) NSString *address2;
@property (nonatomic, retain) NSString *address3;
@property (nonatomic, retain) NSString *phone;
@property (nonatomic, retain) NSString *expires;
@end
そして私の.m:
#import "DOR_RestaurantClass.h"
@implementation DOR_RestaurantClass
@synthesize restId;
@synthesize image;
@synthesize name;
@synthesize description;
@synthesize distance;
@synthesize address;
@synthesize address2;
@synthesize address3;
@synthesize phone;
@synthesize expires;
@end
私は自分のデータなどを取得しているので、それは問題ではありません。私の質問は次のとおりです。XML データに複数の「レストラン」タグがある場合、XML を並べ替えてレストランのデータをまとめるにはどうすればよいですか? 私は PHP から来たので、レストランのタグで foreach を実行したでしょうが、NSXMLParser がどのようにデータを分離しておくべきかわかりません。最後に、この情報を NSMutableArray に入れます。どこから始めればいいのかわからず、これを行うための良い例が見つかりません。どんな助けでも大歓迎です。
これは更新されました
私の配列listItems
(NSMutableArray)はnull
、に出力したときNSLog
です。