如何将xml文件的属性加入NSMutableDictionary?

时间:2012-11-01 16:29:34

标签: iphone xml cocoa-touch nsmutablearray nsmutabledictionary

我必须将解析后的XML文件的所有相同属性加入NSMubableDictionary

我需要关于" show1"," show2" 等所有内容......在字典中(" &# 34; show1"," show2" 等...键和属于每个键的属性为NSArrays

我使用RaptureXML库来解析内容:

RXMLElement *rootXML = [RXMLElement elementFromXMLString:string
                                                   encoding:NSUTF8StringEncoding];

[rootXML iterateWithRootXPath:@"//audio" usingBlock: ^(RXMLElement *program) {

    NSString * show = [NSString stringWithString:[program attribute:@"program"]];
    NSString * date = [NSString stringWithString:[program attribute:@"date"]];
    NSString * time = [NSString stringWithString:[program attribute:@"time"]];
    NSString * path = [NSString stringWithString:[program attribute:@"path"]];

}];

}

这是XML文件:

<audio date="11/01/2012" time="12:00 to 13:00" program="show1" path="http://mydomain/onDemand/audio_12.mp3"/>
<audio date="11/01/2012" time="11:00 to 12:00" program="show2" path="http://mydomain/onDemand/audio_11.mp3"/>
<audio date="11/01/2012" time="10:00 to 11:00" program="show2" path="http://mydomain/onDemand/audio_10.mp3"/>
<audio date="11/01/2012" time="09:00 to 10:00" program="show2" path="http://mydomain/onDemand/audio_09.mp3"/>
<audio date="11/01/2012" time="08:00 to 09:00" program="show3" path="http://mydomain/onDemand/audio_08.mp3"/>
<audio date="11/01/2012" time="07:00 to 08:00" program="show3" path="http://mydomain/onDemand/audio_07.mp3"/>
<audio date="11/01/2012" time="06:00 to 07:00" program="show3" path="http://mydomain/onDemand/audio_06.mp3"/>
<audio date="11/01/2012" time="05:00 to 06:00" program="show4" path="http://mydomain/onDemand/audio_05.mp3"/>
<audio date="11/01/2012" time="04:00 to 05:00" program="show4" path="http://mydomain/onDemand/audio_04.mp3"/>
<audio date="11/01/2012" time="03:00 to 04:00" program="show4" path="http://mydomain/onDemand/audio_03.mp3"/>

我该怎么做?有没有更好的方法来解决这个问题?谢谢!

2 个答案:

答案 0 :(得分:1)

你没有提到你正在使用的XML库,一些伪代码:

NSMutableDictionary * dict = blablah
for(xmlthing * audio in yourxmlarrayofthoseaudiotags)
{
    NSString * key = [audio attributeForKey:@"program"];
    NSArray * showObject = [dict objectForKey:key];
    if(showObject == nil)
    {
        create your base array/dictionary object based on shows
    }
    create your show dictionary object by adding only date, time, path
    [showObject addObject:showtime];
}

编辑: 你提到lib是一件好事,因为它是基于块的,与所有其他xml库完全不同。上面的代码是伪代码,您必须更新并更改使其在代码中运行所需的内容。但无论如何,对于RaptureXML来说,还有以下几点:

NSMutableDictionary * reorgDict = [[NSMutableDictionary alloc] init];
[rootXML iterate:@"audio.*" usingBlock: ^(RXMLElement * element) {
     NSString * key = [element attribute:@"program"];
     NSMutableArray * showObjects = [reorgDict objectForKey:key];
    if(showObjects == nil)
    {
        showObjects = [[NSMutableArray alloc] init];
        [reorgDict setObject:showObjects forKey:key];
    }
    NSMutableDictionary * thisShow = [[NSMutableDictionary alloc] init];
    [thisShow setObject:element attribute:@"date" forKey:@"date"];
    [thisShow setObject:element attribute:@"time" forKey:@"time"];
    [thisShow setObject:element attribute:@"path" forKey:@"path"];
    [showObjects addObject:thisShow];
}];

可能有更聪明的方法来处理那里的处理部分,但我之前没有使用过这个库。但是上面的代码试图做的是重新解析xml并将其放入如下所示的结构中

>dictionary
\-> key = "program" ("show1")
 \-> array
  \-> dictionary
    \-> key = "date"
    \-> key = "time"
    \-> key = "path"

\-> key = "program" ("show2")
 \-> array
  \-> dictionary
    \-> key = "date"
    \-> key = "time"
    \-> key = "path"
  \-> dictionary
    \-> key = "date"
    \-> key = "time"
    \-> key = "path"

答案 1 :(得分:0)

您可以NSXMLParser执行此操作,请检查out。 Apple here提供了一份很好的文档。