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

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

9 Comments

I'm using GDataXML to parse my xml. And following the tutorial at raywenderlich.com/725/…
Also I've looked into Plist's but have no clue how I'd get it to display arrays of my custom classes.
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.
I have the xml -> object code, it's just that I can't seem to save the xml into an array.
I think you'll need to show us some code then! I don't understand what you mean by "Saving into an array".
|
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

1 Comment

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.
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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.