0

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

我需要字典中有关"show1"、"show2"等的所有内容(即 " "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"/>

我怎样才能做到这一点?有没有更好的方法来解决这个问题?谢谢!

4

2 回答 2

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

编辑:您提到该库是一件好事,因为它是基于块的,并且与所有其他 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"
于 2012-11-01T18:54:50.847 回答
0

你可以这样做NSXMLParser,检查一下Apple here有一个很好的文档。

于 2012-11-01T16:33:10.427 回答