大家好解析一个xml文件并保存在一个nsmutabledictionary中,同时重新获取它给出的重复值。
<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
并解析并将resRict中的calcResult字典保存在另一个类中并显示如下。
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"]);
问题是它没有显示保管详细信息......对于保管和非保管,显示NonCustodilal数据......
请帮帮我
感谢
答案 0 :(得分:2)
问题是NSDictionary中的对象是浅层复制的,也就是说在将tempDict
添加到calcResult
之后,它只包含一个指向tempDict
的指针。但是对于字典中的键不是这样,它们被复制并且以后不能修改而不影响字典的完整性。
此外,我想指出您可以使用NSLog(@“resDict = %@“, resDict);
和类似内容转储字典的全部内容。