iphone - Get Correct Output : NSXML Parser -
my xml file :
<?xml version="1.0" encoding="utf-8"?> <plist version="1.0"> <count count="3" /> <spac> <opt>aa</opt> <opt>bb</opt> </spac> </plist> i have used following line of code nsxml parssr :
- (void)parser:(nsxmlparser *)parser didstartelement:(nsstring *)elementname namespaceuri:(nsstring *)namespaceuri qualifiedname:(nsstring *)qualifiedname attributes:(nsdictionary *)attributedict { if([elementname isequaltostring:@"spaces"]) { //initialize array. appdelegate.api = [[nsmutablearray alloc] init]; } } - (void)parser:(nsxmlparser *)parser foundcharacters:(nsstring *)string { [appdelegate.api addobject:string]; nslog(@"the count :%d", [appdelegate.api count]); } - (void)parser:(nsxmlparser *)parser didendelement:(nsstring *)elementname namespaceuri:(nsstring *)namespaceuri qualifiedname:(nsstring *)qname { if([elementname isequaltostring:@"spaces"]) return; } but getting following output @ gdb , i'm unable figure out reason :
2012-06-05 02:20:57.940 xml[490:f803] count :0 2012-06-05 02:20:57.942 xml[490:f803] count :0 2012-06-05 02:20:57.943 xml[490:f803] count :1 2012-06-05 02:20:57.944 xml[490:f803] count :2 2012-06-05 02:20:57.945 xml[490:f803] count :3 2012-06-05 02:20:57.946 xml[490:f803] count :4 2012-06-05 02:20:57.946 xml[490:f803] count :5 2012-06-05 02:20:57.948 xml[490:f803] count :6 2012-06-05 02:20:57.948 xml[490:f803] count :7 2012-06-05 02:20:57.949 xml[490:f803] count :8 can please me out ?? new objective c. thanks.
the reason why have 2 zeros printed out because in first callback create new mutable array when element spaces.
if([elementname isequaltostring:@"spaces"]) { //<- check spaces create array //initialize array. appdelegate.api = [[nsmutablearray alloc] init]; } however, - (void)parser:foundcharacters: called each element characters found in. first nodes found added nil array , print 0. node found after spaces continue adding unwanted characters it. if had xml looked following can see happening.
<xml> <node1>text found gets added nil array (prints 0 count)</node1> <node2>more text found gets added nil array (prints 0 count)</node2> <spaces>this spaces text added array correctly</spaces> <node4>this text added non-nil array , appear spaces</node4> </xml>
Comments
Post a Comment