这个问题可能是重复的问题。但我无法为我的问题找到合适的答案。
我从服务器下载了xml文件。
<INCIDENTTYPES>
<INCIDENT FORMNAME="first" TEXT="First incident">
<TYPE>Type1</TYPE>
<TYPE>Type2</TYPE>
<TYPE>Type3 Tag</TYPE>
<TYPE>Type4 Tag</TYPE>
<LOCATION>Council</LOCATION>
<LOCATION>Domestic</LOCATION>
<LOCATION>Commercial</LOCATION>
</INCIDENT>
<INCIDENT FORMNAME="second" TEXT="Second incident">
<TYPE>Second type</TYPE>
<TYPE>Second/first type</TYPE>
<TYPE>Second3</TYPE>
<LOCATION>Council</LOCATION>
<LOCATION>Domestic</LOCATION>
<LOCATION>Commercial</LOCATION>
</INCIDENT>
</INCIDENTTYPES>
我正在使用RaptureXML来解析xml文件,解析可以按如下方式完成。
RXMLElement *rootXML = [RXMLElement elementFromXMLData:self.connectionData];
[rootXML iterate:@"INCIDENT" usingBlock: ^(RXMLElement *incidents) {
if([[incidents attribute:@"FORMNAME"] isEqualToString:@"first"]){
NSArray *listArray = [NSArray array];
listArray = [incidents children:@"TYPE"];
IncidentData *iData = [[IncidentData alloc] init];
[iData.type setValue:listArray forKey:@"type"];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:iData];
[self.userDefault setObject:data forKey:@"firstObject"];
}
}
IncidentData类如下。
#import "IncidentData.h"
@implementation IncidentData
- (id)initWithCoder:(NSCoder *)aDecoder{
if (self = [super init]) {
//self.type is an array
self.type = [aDecoder decodeObjectForKey:@"type"];
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)aCoder{
[aCoder encodeObject:self.type forKey:@"type"];
}
@end
我正在尝试按以下方式加载数据。
NSData *dd = [self.userDefault objectForKey:@"firstObject"];
IncidentData *items = (IncidentData *)[NSKeyedUnarchiver unarchiveObjectWithData:dd];
NSArray *typeArray = [items.type objectAtIndex:0];
NSLog(@"Incident type : %@", typeArray);
但是typeArray为null。
摘要:我正在尝试解析远程xml文件并将值保存到磁盘上并使用应用程序的后续阶段。
你可以帮我解决这个问题吗?
答案 0 :(得分:1)
RaptureXML,RXMLElement可能不支持NSCoding
尝试代替:
listArray = [incidents children:@"TYPE"];
搭配:
listArray = [[incidents children:@"TYPE"] valueForKeyPath:@"text"];