Reputation: 638
hi all im parsing an xml file and saving in a nsmutabledictionary and while retriving its giving duplicate values.
<CalculateCSResult>
<Message>Hi This is the calculated info</Message>
<Custodial>
<Income>22</Income>
<OverNights>12</OverNights>
<ExemptionAmount>425</ExemptionAmount>
<StandardDeduction>512</StandardDeduction>
<FedTaxableIncome>8569</FedTaxableIncome>
<FederalTax>245</FederalTax>
<StateTax>451</StateTax>
<FICA>451</FICA>
<NetIncome>652</NetIncome>
<NetIncomePct>412542</NetIncomePct>
</Custodial>
<NonCustodial>
<Income>842</Income>
<OverNights>652</OverNights>
<ExemptionAmount>356</ExemptionAmount>
<StandardDeduction>541</StandardDeduction>
<FedTaxableIncome>658</FedTaxableIncome>
<FederalTax>412</FederalTax>
<StateTax>142</StateTax>
<FICA>652</FICA>
<NetIncome>696</NetIncome>
<NetIncomePct>96896</NetIncomePct>
</NonCustodial>
<TaxYear>523</TaxYear>
<ChilsSupportAmt>652</ChilsSupportAmt>
<CutodialChildTaxCredit>652</CutodialChildTaxCredit>
<Alerts>xmlxml</Alerts>
</CalculateCSResult>
#import "ResultParser.h"
@implementation ResultParser
@synthesize calcResult;
NSMutableDictionary *tempDict;
-(void)parse:(NSURL *)url {
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
tempDict = [[NSMutableDictionary alloc]init];
[xmlParser setDelegate:self];
if(![xmlParser parse])
NSLog(@"Error Error Error!!!");
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict {
if ([elementName isEqualToString:@"CalculateCSResult"]) {
calcResult = [[NSMutableDictionary alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if(!currentElementValue)
currentElementValue = [[NSMutableString alloc] initWithString:string];
else
[currentElementValue appendString:string];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if([elementName isEqualToString:@"CalculateCSResult"]) {
return;
}else if([elementName isEqualToString:@"Message"]) {
[calcResult setObject:currentElementValue forKey:elementName];
}else if([elementName isEqualToString:@"Income"] ||
[elementName isEqualToString:@"OverNights"] ||
[elementName isEqualToString:@"ExemptionAmount"] ||
[elementName isEqualToString:@"StandardDeduction"] ||
[elementName isEqualToString:@"FedTaxableIncome"] ||
[elementName isEqualToString:@"FederalTax"] ||
[elementName isEqualToString:@"StateTax"] ||
[elementName isEqualToString:@"FICA"] ||
[elementName isEqualToString:@"NetIncome"] ||
[elementName isEqualToString:@"NetIncomePct"]) {
[tempDict setObject:currentElementValue forKey:elementName];
}else if([elementName isEqualToString:@"NonCustodial"] || [elementName isEqualToString:@"Custodial"]) {
[calcResult setObject:tempDict forKey:elementName];
[tempDict removeAllObjects];
}else if([elementName isEqualToString:@"TaxYear"]) {
[calcResult setObject:currentElementValue forKey:elementName];
}else if([elementName isEqualToString:@"ChilsSupportAmt"]) {
[calcResult setObject:currentElementValue forKey:elementName];
}else if([elementName isEqualToString:@"CutodialChildTaxCredit"]) {
[calcResult setObject:currentElementValue forKey:elementName];
}else if([elementName isEqualToString:@"Alerts"]) {
[calcResult setObject:currentElementValue forKey:elementName];
}
[currentElementValue release];
currentElementValue = nil;
}
- (void)dealloc {
[tempDict release];
[calcResult release];
[super dealloc];
}
@end
and after parsing and im saving the calcResult dictionary in resDict in another class and displaying as below.
NSLog(@" message = %@",[resDict objectForKey:@"Message"]);
NSMutableDictionary *custDict =(NSMutableDictionary *)[resDict objectForKey:@"Custodial"];
NSLog(@" Income = %@",[custDict objectForKey:@"Income"]);
NSLog(@" OverNights = %@",[custDict objectForKey:@"OverNights"]);
NSLog(@" ExemptionAmount = %@",[custDict objectForKey:@"ExemptionAmount"]);
NSLog(@" StandardDeduction = %@",[custDict objectForKey:@"StandardDeduction"]);
NSLog(@" FedTaxableIncome = %@",[custDict objectForKey:@"FedTaxableIncome"]);
NSLog(@" FederalTax = %@",[custDict objectForKey:@"FederalTax"]);
NSLog(@" StateTax = %@",[custDict objectForKey:@"StateTax"]);
NSLog(@" FICA = %@",[custDict objectForKey:@"FICA"]);
NSLog(@" NetIncome = %@",[custDict objectForKey:@"NetIncome"]);
NSLog(@" NetIncomePct = %@",[custDict objectForKey:@"NetIncomePct"]);
NSMutableDictionary *ncustDict =(NSMutableDictionary *)[resDict objectForKey:@"NonCustodial"];
NSLog(@" Income = %@",[ncustDict objectForKey:@"Income"]);
NSLog(@" OverNights = %@",[ncustDict objectForKey:@"OverNights"]);
NSLog(@" ExemptionAmount = %@",[ncustDict objectForKey:@"ExemptionAmount"]);
NSLog(@" StandardDeduction = %@",[ncustDict objectForKey:@"StandardDeduction"]);
NSLog(@" FedTaxableIncome = %@",[ncustDict objectForKey:@"FedTaxableIncome"]);
NSLog(@" FederalTax = %@",[ncustDict objectForKey:@"FederalTax"]);
NSLog(@" StateTax = %@",[ncustDict objectForKey:@"StateTax"]);
NSLog(@" FICA = %@",[ncustDict objectForKey:@"FICA"]);
NSLog(@" NetIncome = %@",[ncustDict objectForKey:@"NetIncome"]);
NSLog(@" NetIncomePct = %@",[ncustDict objectForKey:@"NetIncomePct"]);
NSLog(@" TaxYear = %@",[resDict objectForKey:@"TaxYear"]);
NSLog(@" ChilsSupportAmt = %@",[resDict objectForKey:@"ChilsSupportAmt"]);
NSLog(@" CutodialChildTaxCredit = %@",[resDict objectForKey:@"CutodialChildTaxCredit"]);
NSLog(@" Alerts = %@",[resDict objectForKey:@"Alerts"]);
the problem is its not displaying the Custodial details... for both Custodial and NonCustodial is displaying NonCustodilal data...
please help me out
thanks
Upvotes: 0
Views: 437
Reputation: 452
The problem is that the objects in an NSDictionary are shallow-copied, which is to say that after you add tempDict
to calcResult
, it simply contains a pointer to tempDict
. This is not true for the keys in the dictionary though, which are copied and cannot be modified later without affecting the integrity of the dictionary.
Also, I’d like to point out that you can dump the entire contents of your dictionaries using NSLog(@“resDict = %@“, resDict);
and similar.
Upvotes: 2