Pergunta

This question can be a duplicated one. But I was unable to find a proper answer for my issue.

I have following xml file downloading from a server.

<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>

I am using RaptureXML to parse the xml file and parsing can be done as follows.

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"];
        }
    }

The IncidentData class as follows.

    #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

I am trying to load data as following.

NSData *dd = [self.userDefault objectForKey:@"firstObject"];

IncidentData *items = (IncidentData *)[NSKeyedUnarchiver unarchiveObjectWithData:dd];
NSArray *typeArray = [items.type objectAtIndex:0];
NSLog(@"Incident type : %@", typeArray);

But typeArray is null.

Summary: I am trying to parse a remote xml file and save the values on to the disk and use later stage of the app.

Can you please help me on this ?

Foi útil?

Solução

RaptureXML, RXMLElement might not support NSCoding.
try substituting:
listArray = [incidents children:@"TYPE"];
with:
listArray = [[incidents children:@"TYPE"] valueForKeyPath:@"text"];

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top