0

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
John Lilley
  • 159
  • 1
  • 3
  • 13

3 Answers3

1

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.

Steven Kramer
  • 8,473
  • 2
  • 37
  • 43
  • I'm using GDataXML to parse my xml. And following the tutorial at http://www.raywenderlich.com/725/how-to-read-and-write-xml-documents-with-gdataxml – John Lilley Mar 21 '11 at 12:42
  • Also I've looked into Plist's but have no clue how I'd get it to display arrays of my custom classes. – John Lilley Mar 21 '11 at 12:44
  • Plists cannot contain custom classes. They support, e.g., NSString, NSDate, NSArray. You'll need GDataXML for your classes. There's no quick&easy object<->XML persistence mapping that I know of. – Steven Kramer Mar 21 '11 at 12:50
  • I have the xml -> object code, it's just that I can't seem to save the xml into an array. – John Lilley Mar 21 '11 at 12:58
  • I think you'll need to show us some code then! I don't understand what you mean by "Saving into an array". – Steven Kramer Mar 21 '11 at 13:13
  • I've posted my parser class. When i say i want to add it to an array i mean that. In my xml I have a list of players, each of these players have their own info. I basically want to make an array out of these players. – John Lilley Mar 21 '11 at 13:20
  • I think if you just used the commented out (NSData *xmlData = [NSData dataWithContentsOfFile:filePath];) the XML parsing should be fine. What is not working for you now? – Steven Kramer Mar 21 '11 at 13:26
  • Ok, kind of embarrassed to say it but you are correct and I've found the problem. When I was determining which element to look for in my xml I was using lower case but In my xml each element begins with an upper case letter. It now completely works if I remove the NSMutable array line and replace it with the line I commented out. – John Lilley Mar 21 '11 at 13:33
  • Glad it worked out, I thought it would be simple after looking at your sample which looks quite well coded. – Steven Kramer Mar 21 '11 at 13:48
0

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!

LenArt
  • 111
  • 1
  • 2
0

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

RGML
  • 3,429
  • 2
  • 22
  • 18
  • Yea I've looked at that one before. The main thing that dissuaded me from using it was that it says you can't edit or create xml files. – John Lilley Mar 21 '11 at 14:08