问题描述
- 解析远程xml文件结果为空
- 解析远程的XML文件,然后将值保存到磁盘上。然后下面的步骤:
从服务器下载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返回空值。
请高手帮忙解答,谢谢
解决方案
可能是根本没有获取到内容吧。
时间: 2025-01-31 07:16:45