Reputation: 7764
I have to join all the same atributes of a parsed XML file into an NSMubableDictionary
.
I need everything about "show1", "show2", etc... in a dictionary (being ""show1", "show2", etc... the keys, and the atributes being NSArrays
belonging to each key.
I'm using the RaptureXML library to parse the content:
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"]];
}];
}
Here is the XML file:
<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"/>
How can I do this? Is there a better way to approach this problem? Thank you!
Upvotes: 0
Views: 163
Reputation: 902
Having no hint from you about what XML lib you are using, some pseudocode:
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];
}
Edit: Its a good thing you mention the lib as that one is block based and completely different than all other xml libs. The above code was pseudocode, you have to update and change the things necessary to get it working in your code. But anyway, for RaptureXML something along the lines of:
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];
}];
There may be a more clever way to do the processing part there but I've not used this library before. But what the above code is trying to do is reparse the xml and put it into a structure like the below
>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"
Upvotes: 1
Reputation: 8163
You can do that with NSXMLParser
, check it out. There is a good document about it by Apple here.
Upvotes: 0