如何解析具有许多属性的xml解析器

时间:2010-12-26 20:41:45

标签: iphone

这是我的xml .. formate。

 <events>
    <event id="237" title="HotFridayz" description="Club home / Amsterdam / N" date="17/12/2010" time="23:00 - 05:00" location="Club Nouvelle" street="Uilenstede 9-3018" city="Amstelveen" visitors="10" organisation="VSSA" price="13" minimum_age="" picture="" genre="Hip Hop" lineup="Dj Wily - Waxfiend" website=""/>
    <event id="238" title="Bassline" description="Jimmy woo/ Amsterdam / Nl" date="17/12/2010" time="23:55 - 03:00" location="P60" street="Katrijpstraat 126" city="Amsterdam" visitors="1000" organisation="first priority" price="15" minimum_age="" picture="" genre="Reggae" lineup="Chucky" website=""/>
    ....
    ....
    ....
    </events>

我参与了这段代码......

#import "XMLParser.h"
#import "PartyDemoProjectAppDelegate.h"
#import "CityInfo.h"

@implementation XMLParser
@synthesize currentElementValue;
@synthesize currentElement;

-(XMLParser *)initXMLParser{
    [super init];
    appDelegate = (PartyDemoProjectAppDelegate *)[[UIApplication sharedApplication] delegate];  
    return self;
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName 
    attributes:(NSDictionary *)attributeDict{
    self.currentElement=elementName;
    currentElementValue=@"";
    if ([elementName isEqualToString:@"events"]) {
        appDelegate.cityInfoListArray=[[NSMutableArray alloc]init];

        return;
    }
    if ([elementName isEqualToString:@"event"]) {
    aCityInfo=[[CityInfo alloc] init];
    aCityInfo.event_ID=[attributeDict objectForKey:@"id"];
    aCityInfo.event_Title=[attributeDict objectForKey:@"title"];
    aCityInfo.event_Description=[attributeDict objectForKey=@"description"];
    }
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if ([elementName isEqualToString:@"events"]) {
    return;
}

    if ([elementName isEqualToString:@"event"]) {
        [appDelegate.cityInfoListArray addObject:aCityInfo];
        [aCityInfo release];
        aCityInfo=nil;
NSLog(@"Data",appDelegate.cityInfoListArray);
    }
}


- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { 

//  currentElementValue =  [currentElementValue stringByAppendingString:string];
}

-(void)dealloc{

    [currentElementValue release];
    [currentElement release];
    [super dealloc];
}
@end

当我在控制台中打印时  的NSLog(@ “数据”,appDelegate.cityInfoListArray); 它进行迭代..........

例如: 数据&lt; 0X1&gt;

数据&lt; 0X1&gt; 数据&lt; 0X2&gt;

数据&lt; 0x1&gt; 数据&lt; 0x2&gt; 数据&lt; 0x3&gt; ......

我需要知道这种解析方法很好?????

@Thanks提前...........快乐的圣诞节...向所有人推进新的一年。

1 个答案:

答案 0 :(得分:1)

查看您可能需要登录Apple开发者帐户的Apple示例代码XMLPerformance

我使用相同的方法,我建议使用多线程来解析数据以避免应用中的暂停。