将 xml 解析为 NSMutableDictionary [英] parse xml to NSMutableDictionary

查看:66
本文介绍了将 xml 解析为 NSMutableDictionary的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试将一些 xml 数据解析为 NSDictionary.这是我第一次尝试这样的事情,有点不确定自己在做什么,

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,

首先,我正在解析一些大致如下所示的 XML -

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>

我正在使用 NSXMLParser 委托,所以使用 NSLog

I am using the NSXMLParser delegates, so using a NSLog

NSLog(@"attributes: %@", attributeDict);

parser:didStartElement:namespaceURI:qualifiedName:attributes: 委托方法上,我的输出看起来像这样.

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

我现在希望将这些内容解析为 NSMutableDictionary 但我不太确定如何解决这个问题...我希望做这样的事情

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

我的问题变成这样看起来对吗?然后如果是这样我如何在头文件中声明这个可变字典?以及如何为此 NSMutableDictionary

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

推荐答案

你必须区分 NSObject 的子类和 NSMutableDictionary 的子类.

You have to distinguish between a subclass of NSObject and a NSMutableDictionary.

按照您编写的方式,它似乎是一个具有某些属性的对象.如果您的 XML rows 始终包含相同的字段,我认为这是最好的方法.你的对象的属性是自动可变的",所以从这方面来说没有什么可担心的.

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];
...

这篇关于将 xml 解析为 NSMutableDictionary的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆