2013-08-16 132 views
0

我需要從webservicex.net解析以下xml for和iPhone App。我需要將VerseBibleWords的經文存儲到某些NSArray/NSDictionary或其他東西中。麻煩解析XML和提取數據

<string xmlns="http://www.webserviceX.NET"> 
<NewDataSet> 
    <Table> 
    <Book>1</Book> 
    <BookTitle>Genesis</BookTitle> 
    <Chapter>1</Chapter> 
    <Verse>1</Verse> 
    <BibleWords>In the beginning God created the heaven and the earth.</BibleWords> 
    </Table> 
    <Table> 
    <Book>1</Book> 
    <BookTitle>Genesis</BookTitle> 
    <Chapter>1</Chapter> 
    <Verse>2</Verse> 
    <BibleWords>And the earth was without form, and void; and darkness was upon the face of the deep. And the Spirit of God moved upon the face of the waters.</BibleWords> 
    </Table> 
    <Table> 
    <Book>1</Book> 
    <BookTitle>Genesis</BookTitle> 
    <Chapter>1</Chapter> 
    <Verse>3</Verse> 
    <BibleWords>And God said, Let there be light: and there was light.</BibleWords> 
    </Table> 
    <Table> 
    <Book>1</Book> 
    <BookTitle>Genesis</BookTitle> 
    <Chapter>1</Chapter> 
    <Verse>4</Verse> 
    <BibleWords>And God saw the light, that it was good: and God divided the light from the darkness.</BibleWords> 
    </Table> 
</NewDataSet> 
</string> 

我寫了下面的代碼來解析數據。

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict { 
    if ([elementName isEqualToString:@"string"]) { 
     model = [[Model alloc]init];   
    } 
} 

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { 
    if(!currentElementValue) { 
     currentElementValue = [[NSMutableString alloc]initWithString:string]; 
    } 

    else { 
     [currentElementValue appendString:string]; 
    } 

    NSLog(@"%@", currentElementValue); 
} 

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName { 
    if ([elementName isEqualToString:@"NewDataSet"]) { 
     return; 
    } 

    if ([elementName isEqualToString:@"Table"]) { 
     [verses addObject:model]; 
     model = nil; 
    } 
    else { 
     [model setValue:currentElementValue forKey:elementName]; //The exception break point hits here 
    }  

    currentElementValue = nil; 
} 

currentElementValue顯示正確。 model是和類名爲Model,currentElementValue的對象是NSMutableString對象versesNSMutableArray。我是目標C的初學者,以前沒有做過解析。問題是,該方法:

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName 

未按預期工作。點擊我在代碼中評論的異常斷點後,該應用程序中斷。我如何獲得VerseBibleWords的值並將其存儲到某些NSArray/NSMutableArray/NSDictionary/NSMutableDictionary。請詢問是否還有其他事情需要提及,以確定問題實際存在的位置。

編輯:

試過這樣:

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict { 
    captureCharacters = NO; 
    [currentElementValue setString : @""]; 
    if ([elementName isEqualToString:@"Table"]) { //set a break point here as well. The elementName is string and hence is not entering any of the conditions in this delegate method. Hence captureCharacters is always set to NO and nothing is working. 
     model = [[Model alloc]init];   
    } 
    else if ([elementName isEqualToString:@"Verse"]) { 
     capturingCharacters = yes; 
    } 

    else if ([elementName isEqualToString:@"BibleWords"]) { 
     capturingCharacters = yes; 
    } 
} 

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { 
    if(capturingCharacters) { 
     [currentElementValue appendString:string]; 
    } 
} 

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName { 
    if ([elementName isEqualToString:@"Table"]) { 
     [array addObject : model]; 
     model = nil;   
    } 
    else if ([elementName isEqualToString:@"Verse"]) { //set a break point here 
     model.Verse = currentElementValue; //not entering this condition 
    } 

    else if ([elementName isEqualToString:@"BibleWords"]) { 
     model.BibleWords = currentElementValue; //not entering this condition 
    }   
} 

它不工作,我對model.Versemodel.BibleWords越來越空值。 這些方法是如何工作的?

EDIT-2
模型接口:

//model.h 
@protocol HttpRequestHandler <NSObject> 

- (void)returnFromSiteParsedData:(NSMutableArray *)parsedData; 

@end 

@interface Model : NSObject <NSURLConnectionDelegate, NSXMLParserDelegate> 
{ 
    NSString *urlToLoad; 
    NSMutableURLRequest *requestSiteToSendData; 
    NSURLConnection *connectionToSiteToSendData; 

    NSString *verseText; 
    int verseNumber; 

} 
@property(nonatomic, retain) NSString *verseText; 
@property(nonatomic, assign) int verseNumber; 

- (void)loadSiteToSendDataWithChapterNumber:(int)chapterNumber AndBookNumber:(int)bookNumber; 

@property(nonatomic, retain)id<HttpRequestHandler>httpRequestHandlerDelegate; 

@end 

模型實現

//model.m 
@implementation Model 

@synthesize verseNumber; 
@synthesize verseText; 

- (void)loadSiteToSendDataWithChapterNumber:(int)chapterNumber AndBookNumber:(int)bookNumber { 

    urlToLoad = [[NSString alloc]initWithFormat:@"http://www.webservicex.net/BibleWebservice.asmx/GetBibleWordsByBookTitleAndChapter?BookTitle=Genesis&chapter=1"]; 
    requestSiteToSendData = [NSMutableURLRequest requestWithURL:[[NSURL alloc]initWithString:urlToLoad] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30]; 
    connectionToSiteToSendData = [[NSURLConnection alloc]initWithRequest:requestSiteToSendData delegate:self];  

} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 

    // Parse the XML into a dictionary 
    NSXMLParser *parser = [[NSXMLParser alloc]initWithData:data]; 
    VBSParser *vbsParser = [[VBSParser alloc]initVBSParser]; 
    [parser setDelegate:vbsParser]; 
    BOOL success = [parser parse]; 

    if (success) { 
     NSMutableArray *verses = [vbsParser verses]; 
     NSLog(@"%@", verses); 
     NSLog(@"Number: %d\n Text: %@", verseNumber,verseText); 
     [self.httpRequestHandlerDelegate returnFromSiteParsedData:verses]; 
    } 
    // Print the dictionary 

} 

@end 

委託用於解析的數據返回到視圖控制器。

+0

請參閱我的編輯。 –

+0

讓我看看你的模型班 –

+0

請參閱編輯。 –

回答

1

make BOOL capturingCharacters;

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict { 
    captureCharacters = NO; 
    [currentElementValue setString : @""]; 
    if ([elementName isEqualToString:@"Table"]) { 
     model = [[Model alloc]init];   
    } 
    else if ([elementName isEqualToString:@"Verse"]) { 
     capturingCharacters = yes; 
    } 

    else if ([elementName isEqualToString:@"BibleWords"]) { 
     capturingCharacters = yes; 
    } 
} 

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { 
    if(capturingCharacters) { 
     [currentElementValue appendString:string]; 
    } 
} 

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName { 
    if ([elementName isEqualToString:@"Table"]) { 
     [array addObject : model]; 
     model = nil;   
    } 
    else if ([elementName isEqualToString:@"Verse"]) { 
     model.Verse = currentElementValue; 
    } 

    else if ([elementName isEqualToString:@"BibleWords"]) { 
     model.BibleWords = currentElementValue; 
    }   
}