2013-01-14 29 views
0

這是我需要解析iOS版 - 解析xml數據集,

<GetMessagesResult> 
    <NewDataSet xmlns=""> 
     <Table> 
      <Date>21:52:59</Date> 
      <Message>ABC</Message> 
      <GroupName>ALL</GroupName> 
     </Table> 
     <Table> 
      <Date>11:23:27</Date> 
      <Message>DEF</Message> 
      <GroupName>ALL</GroupName> 
     </Table> 
    </NewDataSet> 
</GetMessagesResult> 

我的XML數據這是我SCMessages.h文件

#import <Foundation/Foundation.h> 
@interface SCMessages : NSObject 
{ 
    NSDate *Date; 
    NSString *Message; 
    NSString *GroupName; 
} 
@property (nonatomic, retain) NSDate *Date; 
@property (nonatomic, retain) NSString *Message; 
@property (nonatomic, retain) NSString *GroupName; 

,這是我SCMessages.m文件

#import "SCMessages.h" 
@implementation SCMessages 
@synthesize Date; 
@synthesize Message,GroupName; 
- (void)dealloc 
{ 
    [super dealloc]; 
    [Date release]; 
    [Message release]; 
    [GroupName release]; 
} 
@end 

我用下面的代碼來解析數據使用NSXMLParser委託方法

#pragma mark - NSXMLParser Delegate 

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *)qName 
    attributes: (NSDictionary *)attributeDict 
{ 
    if([elementName isEqualToString:@"GetMessagesResult"]) 
    { 
     if(!soapResults) 
     soapResults = [[NSMutableString alloc] init]; 
     recordResults = TRUE; 
    } 

    if([elementName isEqualToString:@"NewDataSet"]) { 
     //Initialize the array. 
     messages = [[NSMutableArray alloc] init]; 
    } 
    else if([elementName isEqualToString:@"Table"]) { 
     //Initialize the message. 
     aMessage = [[SCMessages alloc] init]; 
    } 
} 
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string 
{ 
    if(recordResults) 
     [soapResults appendString: string]; 
} 
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName 
{ 
    if([elementName isEqualToString:@"Table"]) { 
     [messages addObject:aMessage]; 
      NSLog(@"MESSAGES COUNT: %d",messages.count); 
      NSLog(@"MESSAGE: %@",aMessage); 
     [aMessage release]; 
     aMessage = nil; 
    } 
    // as this is the last element 
    if([elementName isEqualToString:@"NewDataSet"]) 
    { 
     recordResults = FALSE; 
    } 
} 

問題我不與日期獲取所需信息的對象,Meesage &組名。

我把NSLog打印出來,但我總是得到空值。奇怪的是消息數組獲取分配內存&元素也被添加到數組中,因爲我可以看到消息數組數爲NSLog,但數組元素的數據爲空值。

我解析在NSURLConnection的委託方法connectionDidFinishLoading

NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding]; 
    NSLog(@"theXML: \n%@",theXML); 
+0

謝謝。你明白了嗎? – Nirav

+0

@Nirav。嘿謝謝兄弟。我明白了。 –

回答

0

您需要將您的值存儲在aMessage中。您已將特定對象添加到數組中,但您忘記將數據存儲在SCMessages類的該對象中。您還可以在調試時檢查您在aMessage對象中獲得的值。

您需要在此didEndElement方法中存儲像這樣的值。

[aMessage setValue: soapResults forKey:elementName]; 

我想這會幫助你。

0

你didEnd方法缺少代碼積累的字符串存儲到一個屬性在SOAP響應中的XML數據。您需要爲當前的SCMessage對象提供ivar。

將文本內容追加到可變字符串伊娃。然後關閉一個元素,你必須決定在SCMessage上設置哪個屬性。您可能必須將日期解析到NSDate中。

+0

我無法理解:(你可以詳細說明代碼!! –

+0

對不起,我沒有時間爲你寫代碼,而且我在iPad上,你錯過了一個放置代碼的地方在屬性中找到字符,並且對於每個表元素,你需要創建一個新的消息對象。 – Cocoanetics