2013-10-10 65 views
0

我的XML格式是這樣創建磁盤陣列:從XML文件

<Rows> 
    <Row> 
    <style>String</style? 
    <thumbnail>String</thumbnail> 
    </Row> 
    <Row> 
    <style>String</style? 
    <thumbnail>String</thumbnail> 
    </Row> 
</Rows> 

如何創建一個NSMutableArray這樣的({風格=「值」;縮略圖=「值」;} ... ..)

這裏是我的代碼:

-(void) parser:(NSXMLParser *) parser didStartElement:(NSString *) elementName namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict { 
    if ([elementName isEqualToString:@"style"]) { 
     element = [NSString stringWithString:elementName]; 
     if (!soapResults) { 
      soapResults = [[NSMutableString alloc] init]; 
     } 
     elementFound = YES; 
    } 
    if ([elementName isEqualToString:@"thumbnail1"]) { 
     element = [NSString stringWithString:elementName]; 
     if (!soapResults) { 
      soapResults = [[NSMutableString alloc] init]; 
     } 
     elementFound = YES; 
    } 
    } 

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

{ 
    if (elementFound) { 
     [soapResults appendString:string]; 
    } 
} 

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI: (NSString*)namespaceURI qualifiedName:(NSString *)qName 
{ 
    if ([elementName isEqualToString:@"style"]) { 
     [rowDict setValue:soapResults forKey:@"style"]; 
     elementFound = NO; 
     soapResults = nil; 
    } 
    if ([elementName isEqualToString:@"thumbnail1"]) { 
     [rowDict setValue:soapResults forKey:@"thumbnail1"]; 
     elementFound = NO; 
     soapResults = nil; 
    } 
} 

我越來越如果我做這樣的格式錯誤,請幫我在廣告

謝謝萬斯。

回答

0

創建一個對象類讓利說SampleObj

* SampleObj.h *

@interface SampleObj : NSObject 
{ 

    NSString *style; 
    NSString *thumbnail; 

} 

@property (nonatomic,retain) NSString *style,*thumbnail; 

* SampleObj.m *

@implementation SampleObj 
@synthesize style,thumbnail; 

//dealloc your synthesized value; 

做解析的源代碼像這樣。

NSArray *array=[theXML componentsSeparatedByString:@"<Rows>"]; 
for(int i=1;i<[array count];i++) 
{ 
    NSArray *arrayRow=[theXML componentsSeparatedByString:@"<Row>"]; 

    for(int j=1;j<[arrayRow count];j++) 
    { 
     SampleObj *custobj = [[SampleObj alloc] init]; 
     NSString *str=[arrayRow objectAtIndex:i]; 
     NSArray *arr1=[str componentsSeparatedByString:@"<style>"]; 

     NSString *data=[arr1 objectAtIndex:1]; 
     NSRange ranfrom=[data rangeOfString:@"</style>"]; 
     custobj.style =[data substringToIndex:ranfrom.location]; 

     arr1=[str componentsSeparatedByString:@"<thumbnail>"]; 
     data=[arr1 objectAtIndex:1]; 
     ranfrom=[data rangeOfString:@"</thumbnail >"]; 
     custobj.thumbnail=[data substringToIndex:ranfrom.location]; 

     [yourarray addObject:custobj]; 

     [custobj release] 


     } 
} 

,如果你想使用對象:

SampleObj *custobj = [yourarray objectAtIndex:rowPosition]; 
NSLog(@"style : %@", custobj.style); 
NSLog(@"thumbnail : %@", custobj.thumbnail); 

希望這將有助於。對於任何錯字,感到抱歉。