Creating NSMutableArray from xml file

2.4k views Asked by At

I have an xml file with the following information.

<?xml version="1.0"?>
-<Party> 
-<Player> 
<Name>Butch</Name> 
<Level>1</Level> 
<Class>Fighter</Class> 
</Player> 
-<Player> 
<Name>Shadow</Name> 
<Level>2</Level> 
<Class>Rogue</Class> 
</Player> 
-<Player> 
<Name>Crak</Name> 
<Level>3</Level> 
<Class>Wizard</Class> 
</Player> 
</Party>

I try to add this to an NSMutableArray by doing the following.

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Party" ofType:@"xml"];
NSData *xmlData = [[NSMutableArray alloc] initWithContentsOfFile:filePAth];

When I run this, my array object has no data in it.

If I replace the line with;

NSdata *xmlData = [NSdata dataWithContentsOfFole:filePath];

it will load my data but it is not in the format I require.

I'm very new to Objective-c and iPhone development and any help will be greatly appreciated. If anymore info is needed I'll be happy to provide it.

edit - I'm using GDataXML to parse my xml

edit - My Parser class is as follows

@implementation PartyParser

+(NSString *)dataFilePath:(BOOL)forSave{

    return [[NSBundle mainBundle] pathForResource:@"Party" ofType:@"xml"];
}

+(Party *)loadParty{

    NSString *filePath = [self dataFilePath:FALSE];
    //NSData *xmlData = [NSData dataWithContentsOfFile:filePath];
    NSData *xmlData = [[NSMutableArray alloc] initWithContentsOfFile:filePath];
    NSError *error;
    GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:xmlData options:0 error:&error];
    if(doc ==nil){return nil;}

    NSLog(@"%@", doc.rootElement);

    Party *party = [[[Party alloc] init] autorelease];
    NSArray *partyMembers = [doc.rootElement elementsForName:@"Player"];
    for (GDataXMLElement *partyMember in partyMembers)
    {

        NSString *name;
        int level;
        RPGClass rpgClass;

        //Name
        NSArray *names = [partyMember elementsForName:@"name"];
        if(names.count > 0)
        {
            GDataXMLElement *firstName = (GDataXMLElement *) [names objectAtIndex:0];
            name = firstName.stringValue;
        }else continue;

        //Level
        NSArray *levels = [partyMember elementsForName:@"level"];
        if(levels.count > 0)
        {
            GDataXMLElement *firstLevel = (GDataXMLElement *) [levels objectAtIndex:0];
            level = firstLevel.stringValue.intValue;
        }else continue;

        //Class
        NSArray *classes = [partyMember elementsForName:@"class"];
        if(classes.count > 0)
        {
            GDataXMLElement *firstClass = (GDataXMLElement *) [classes objectAtIndex:0];
            if([firstClass.stringValue caseInsensitiveCompare:@"fighter"] == NSOrderedSame)
            {
                rpgClass = RPGClassFighter;
            }
            else if([firstClass.stringValue caseInsensitiveCompare:@"rogue"] == NSOrderedSame)
            {
                rpgClass = RPGClassRogue;
            }
            else if([firstClass.stringValue caseInsensitiveCompare:@"wizard"] == NSOrderedSame)
            {
                rpgClass = RPGClassWizard;
            }
            else
            {
                continue;
            }
        }else continue;

        Player *player = [[[Player alloc] initWithName:name level:level rpgClass:rpgClass] autorelease];
        [party.players addObject:player];
    }


    //NSLog(@"%@", doc.rootElement);
    //
    [doc release];
    [xmlData release];
    return party;
}

@end
3

There are 3 answers

9
Steven Kramer On BEST ANSWER

The XML file can not be any type of XML. It needs to be in the "property list" format.

Take a look at some Apple docs or some of the .plist files that will be in your project. That will show you how to create such a file.

Alternatively, create one using the "PList Editor" application that comes with the developer tools. It has a good UI for creating these files.

Lastly, your line "NSData* xmlData = ..." is very wrong - you're assigning a mutable array object to an NSData* variable. The types are not interchangeable by a long shot.

If you want to do full-blown XML parsing, there are various options that deserve a topic of their own. The XML used for plists is limited to Cocoa collections and simple data types.

0
LenArt On

Just place your XML file (e.g. "myFileName.xml") anywhere in your project.

Read the xml file contents to NSData using:

NSString *myBundleString = [[NSBundle mainBundle] pathForResource:@"myFileName" ofType:@"xml"];
NSData *myData = [NSData dataWithContentsOfFile:myBundleString];

And parse your data using NSXMLParser:

NSXMLParser *myParser = [[NSXMLParser alloc] initWithData:myData];
[myParser setDelegate:self];
[myParser parse];

Happy parsing using the delegate methods!

1
RGML On

Take a look to this XML Parser. It is called "TBXML", I have used that for a while and I'm quite happy with the result.

http://www.tbxml.co.uk/TBXML/TBXML_Free.html