问题
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
回答1:
You have to distinguish between a subclass of NSObject
and a NSMutableDictionary
.
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:
@interface MyDic : NSObject {
BOOL isManuf;
BOOL isSer;
NSString *manf;
NSUInteger id;
}
@property (nonatomic, assign) BOOL isManuf;
@property (nonatomic, assign) BOOL isSer;
@property (nonatomic, retain) NSString *manf;
@property (nonatomic, assign) NSUInteger id;
@end
MyDic.m
@implementation MyDic
@synthetize isManuf, isSer, manf, id;
-(void)dealloc {
[manf release];
[super dealloc];
}
@end
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.
#import "MyDic.h"
....
myDic = [[MyDic alloc] init];
...
回答2:
You don't want .isManuf, .isSer etc... on the dictionary.
You can:
- create an objective-c class with .isManif, isSer properties (use @property and synthesize). Although, I would recommend more readable names rather than abbreviated names - doesn't need to match xml.
- In the didStartElement method, alloc init your custom object and assign the properties.
- Since you get back n of them, you should create an NSMutableArray in the init of that class (and defined in header). Add the item you just created to that mutable array.
- When your done parsing, you have a mutable array of all the objects in the xml.
来源:https://stackoverflow.com/questions/7719246/parse-xml-to-nsmutabledictionary