I am currently trying to parse some xml data into an NSDictionary
. This is the first time I have tried something like this and am a little bit unsure of what I am doing,
First off I am parsing some XML that looks roughly like this -
<Rows>
<Row ID="76" MANF="JARD" ISMANUF="F" ISSER="F"/>
<Row ID="38" MANF ="SANBIN" ISMANUF ="F" ISSER ="T"/>
<Rows>
I am using the NSXMLParser
delegates, so using a NSLog
NSLog(@"attributes: %@", attributeDict);
on parser:didStartElement:namespaceURI:qualifiedName:attributes:
delegate method and my output looks like this.
}
2011-10-11 08:01:15.472 Code[526:207] attributes: {
ISMANUF = F;
ISSER = T;
MANF = smart;
ID = 74;
}
2011-10-11 08:01:15.472 Code[526:207] attributes: {
ISMANUF = F;
ISSER = T;
MANF = "weather guard";
ID = 71;
}
I am now looking to parse this stuff into a NSMutableDictionary
but am not toatly sure on how to go about this... I am looking to do something like this
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict {
if ([elementName isEqualToString:@"row"]) {
myDic = [[myDic alloc] init];
myDic.isManuf = [attributeDict objectForKey:@"ISMANUF"];
myDic.isSer = [attributeDict valueForKey:@"ISSER"];
myDic.Manf = [attributeDict valueForKey:@"MANF"]
myDic.id = [attributeDict valueForKey:@"ID"]
}
}
My question becomes dose this look right? and then if so how do I declare this mutable dictionary in the header file? and also how to I declared .isManuf .isSer .Manf .id for this NSMutableDictionary
You don't want .isManuf, .isSer etc... on the dictionary.
You can:
You have to distinguish between a subclass of
NSObject
and aNSMutableDictionary
.The way you wrote it, it seesm to be an object with some attributes. If youre XML
rows
contain always the same fields, I think this is the best way. The attributes of your object are automatically "mutable", so there is nothing to worry about from this side.It is better to use custom objects because there is less probility of an error with misspelled keys, and the code is generally more readable.
So you still need to knwo how to declare your object:
MyDic.h:
MyDic.m
And don't forget to import the custom object in your main class and use the class name (with capital initial) when you create the object.