2 bluetooth99 bluetooth99 于 2013.04.02 10:27 提问

解析远程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返回空值。
请高手帮忙解答,谢谢

1个回答

wangyaninglm
wangyaninglm   Ds   Rxr 2015.01.14 22:49

可能是根本没有获取到内容吧。

Csdn user default icon
上传中...
上传图片
插入图片