objective c - Trouble Parsing XML and fetching data -
i have need parse following xml webservicex.net, , iphone app. need store verse verse , biblewords nsarray/nsdictionary or something.
<string xmlns="http://www.webservicex.net"> <newdataset> <table> <book>1</book> <booktitle>genesis</booktitle> <chapter>1</chapter> <verse>1</verse> <biblewords>in beginning god created heaven , earth.</biblewords> </table> <table> <book>1</book> <booktitle>genesis</booktitle> <chapter>1</chapter> <verse>2</verse> <biblewords>and earth without form, , void; , darkness upon face of deep. , spirit of god moved upon face of waters.</biblewords> </table> <table> <book>1</book> <booktitle>genesis</booktitle> <chapter>1</chapter> <verse>3</verse> <biblewords>and god said, let there light: , there light.</biblewords> </table> <table> <book>1</book> <booktitle>genesis</booktitle> <chapter>1</chapter> <verse>4</verse> <biblewords>and god saw light, good: , god divided light darkness.</biblewords> </table> </newdataset> </string> i wrote following code parsing data.
- (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; } the currentelementvalue displayed correctly. model , object of class named model, currentelementvalue nsmutablestring object verses nsmutablearray. beginner in objective c , haven't done parsing before. problem method:
- (void)parser:(nsxmlparser *)parser didendelement:(nsstring *)elementname namespaceuri:(nsstring *)namespaceuri qualifiedname:(nsstring *)qname is not working expected. app got broken after hitting exception break point have commented in code. how can value of verse , biblewords , store nsarray/nsmutablearray/nsdictionary/nsmutabledictionary. please ask if there else need mention see problem lies.
edit:
tried this:
- (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 break point here well. elementname string , hence not entering of conditions in delegate method. hence capturecharacters set no , nothing 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 break point here model.verse = currentelementvalue; //not entering condition } else if ([elementname isequaltostring:@"biblewords"]) { model.biblewords = currentelementvalue; //not entering condition } } it not working , getting null values model.verse , model.biblewords. how these methods work?
edit-2
model interface:
//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 implementation
//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 xml 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 dictionary } @end a delegate used return parsed data view controller.
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; } }
Comments
Post a Comment