NSXMLParser只读取一半属性

时间:2011-03-06 01:12:19

标签: objective-c cocoa nsxmlparser

我只得到前五个值,其余的都是null。

这是XML:

<?xml version="1.0"?>
       <ResourceManifest>
           <Resources id="princessanimation">
               <SetDefaults path="images" imageprefix="card_">
                    <Atlas id="1">
                        <?xml version="1.0"?>
                            <ResourceManifest>
                                <Resources id="cardlist">
                                    <SetDefaults path="images" imageprefix="card_">
                                    <Atlas id="1">
                                        <AtlasEntry id="card1" x="0" y="950.0" w="180" h="250", a="2", d="3", name="Teresa Voldheart", team="horde" />
                                        <AtlasEntry id="card2" x="185.0" y="950.0" w="180" h="250", a="3", d="3", name="Nurgle Tinkfrost", team="alliance"/>
                                        <AtlasEntry id="card3" x="368.0" y="950.0" w="180" h="250", a="3", d="6", name="Nethermaven Donna Chastain", team="alliance"/>
                                        <AtlasEntry id="card4" x="550.0" y="950.0" w="180" h="250", a="2", d="3", name="Vindicator Bellan", team="alliance" />
                                        <AtlasEntry id="card5" x="735.0" y="950.0" w="180" h="250", a="3", d="2", name="Blizzaz", team="alliance"/>
                                        <AtlasEntry id="card6" x="917.0" y="950.0" w="180" h="250", a="3", d="3", name="'Fungus Face' McGuillicutty", team="horde" />
                                        <AtlasEntry id="card7" x="1097.0" y="950.0" w="180" h="250", a="2", d="1", name="Magister Ashi", team="horde" />
                                        <AtlasEntry id="card8" x="1277.0" y="950.0" w="180" h="250", a="2", d="3", name="Vesperia Silversong", team="alliance" />
                                        <AtlasEntry id="card9" x="1470.0" y="950.0" w="180" h="250", a="5", d="5", name="Conqueror Yun'zon", team="horde" />
                                        <AtlasEntry id="card10" x="0.0" y="694.0" w="180" h="250", a="2", d="1", name="Scaramanga", team="alliance" />
                                        <AtlasEntry id="card11" x="185.0" y="694.0" w="180" h="250", a="2", d="5", name="Commander Falstaav", team="alliance" />
                                        <AtlasEntry id="card12" x="368.0" y="694.0" w="180" h="250", a="3", d="6", name="Lilnas the Calm", team="alliance" />
                                        <AtlasEntry id="card13" x="550.0" y="694.0" w="180" h="250", a="2", d="1", name="Routeen", team="alliance" />
                                        <AtlasEntry id="card14" x="735.0" y="694.0" w="180" h="250", a="1", d="1", name="Retainer Kedryn", team="alliance" />
                                        <AtlasEntry id="card15" x="917.0" y="694.0" w="180" h="250", a="1", d="4", name="Lady Courtney Noel", team="alliance" />
                                        <AtlasEntry id="card16" x="1097.0" y="694.0" w="180" h="250", a="5", d="5", name="Osha Shadowdrinker", team="horde" />
                                        <AtlasEntry id="card17" x="1277.0" y="694.0" w="180" h="250", a="6", d="5", name="Vanda Skydaughter", team="horde" />
                                        <AtlasEntry id="card16" x="1470.0" y="694.0" w="180" h="250", a="3", d="3", name="'Posion Tongue' McGillicutty", team="horde" />
                                    </Atlas>
                                </SetDefaults>
                            </Resources>
                        </ResourceManifest>
                    </Atlas>
                </SetDefaults>
            </Resources>
        </ResourceManifest>

以下是代码:

- (void) awakeFromNib { 

//center app
[winMain center];

//allocate cards
cards = [[NSMutableArray alloc]init];
cardSet = [[NSMutableDictionary alloc] init];

//declare a temp array, we'll overwrite this each time we call tFire from the startanimation method
NSArray* tempArray = [[NSMutableArray alloc]init];

//build a path to the xml file
pathToXML = [[NSBundle mainBundle]pathForResource:@"wowCards2" ofType:@"xml"];
//call method to parse xml file and pass path to xml file to it
[self parseXMLFile:pathToXML];

//store each card and it's data  in the cardSet dictionary
for(int c=0; c<[cards count]; c++) { 

    NSString* myAtlasEntry = [cards objectAtIndex:c];
    //take those values, which will be delimited by a "," and place them into our temp array
    tempArray = [myAtlasEntry componentsSeparatedByString:@", "];

    NSString* cardNumber = [NSString stringWithFormat: @"card%i", c+1];
    [cardSet setValue:tempArray forKey: cardNumber];
}

NSLog(@"%@", cards); 
}

- (void) parseXMLFile:(NSString *)pathToFile {

//set a boolean
BOOL success;

//set url to xml file
NSURL *xmlURL = [NSURL fileURLWithPath:pathToFile];

// addressParser is an NSXMLParser instance variable
if (addressParser) {
    [addressParser release];
}

addressParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];
[addressParser setDelegate:self];
[addressParser setShouldResolveExternalEntities:YES];

// return value not used
success = [addressParser parse];
// if not successful, delegate is informed of error

//go to -(void)parser method
}


/*in the parseXMLFile we alloc addressParser as an NSXMLParser, we then assign it a delegate, which in this case is "self" which means the entire view (super view?) is the delegate. So somehow, because of that, this method is automagically called (I think we get here from the [addressParser parse] call in the parseXMLFile method) and we can populate our array with the data from the xml file*/
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {

if ( [elementName isEqualToString:@"AtlasEntry"]) {

    // addresses is an NSMutableArray instance variable
    if (!addresses) {
        addresses = [[NSMutableArray alloc] init];
    }

    NSString *thisOwner = [attributeDict objectForKey:@"id"];
    NSString* theX = [attributeDict objectForKey:@"x"];
    NSString* theY = [attributeDict objectForKey:@"y"];
    NSString* theWidth = [attributeDict objectForKey:@"w"];
    NSString* theHeight = [attributeDict objectForKey:@"h"];
    NSString* charAttack = [attributeDict objectForKey:@"a"];
    NSString* charDefense = [attributeDict objectForKey:@"d"];
    NSString* charName = [attributeDict objectForKey:@"name"];
    NSString* charTeam = [attributeDict objectForKey:@"team"];

    if (thisOwner) {
        [cards addObject:[NSString stringWithFormat:@"%@, %@, %@, %@, %@, %@, %@, %@, %@",thisOwner, theX, theY, theWidth, theHeight, charAttack, charDefense, charName, charTeam]];
    }

    }
}

这是我的日志(NSMutableDictionary卡片):

"card1, 0, 950.0, 180, 250, (null), (null), (null), (null)",
"card2, 185.0, 950.0, 180, 250, (null), (null), (null), (null)",
"card3, 368.0, 950.0, 180, 250, (null), (null), (null), (null)",
"card4, 550.0, 950.0, 180, 250, (null), (null), (null), (null)",
"card5, 735.0, 950.0, 180, 250, (null), (null), (null), (null)",
"card6, 917.0, 950.0, 180, 250, (null), (null), (null), (null)",
"card7, 1097.0, 950.0, 180, 250, (null), (null), (null), (null)",
"card8, 1277.0, 950.0, 180, 250, (null), (null), (null), (null)",
"card9, 1470.0, 950.0, 180, 250, (null), (null), (null), (null)",
"card10, 0.0, 694.0, 180, 250, (null), (null), (null), (null)",
"card11, 185.0, 694.0, 180, 250, (null), (null), (null), (null)",
"card12, 368.0, 694.0, 180, 250, (null), (null), (null), (null)",
"card13, 550.0, 694.0, 180, 250, (null), (null), (null), (null)",
"card14, 735.0, 694.0, 180, 250, (null), (null), (null), (null)",
"card15, 917.0, 694.0, 180, 250, (null), (null), (null), (null)",
"card16, 1097.0, 694.0, 180, 250, (null), (null), (null), (null)",
"card17, 1277.0, 694.0, 180, 250, (null), (null), (null), (null)",
"card18, 1470.0, 694.0, 180, 250, (null), (null), (null), (null)"

我在这里做错了吗?

2 个答案:

答案 0 :(得分:2)

您的AtlasEntry项目无效XML。你的属性之间不能有逗号;这导致了一个解析错误,这就是你得到空值的原因。

例如,

<AtlasEntry id="card16" x="1470.0" y="694.0" w="180" h="250", a="3", d="3", name="'Posion Tongue' McGillicutty", team="horde" />

应该是

<AtlasEntry id="card16" x="1470.0" y="694.0" w="180" h="250" a="3" d="3" name="'Posion Tongue' McGillicutty" team="horde" />

修复所有AtlasEntry项目,你应该好好去。

答案 1 :(得分:2)

<AtlasEntry id="card1" x="0" y="950.0" w="180" h="250", a="2", d="3", name="Teresa Voldheart", team="horde" />

这不是有效的XML,属性之间的逗号不应该存在,请查看当达到第一个逗号时解析器的值如何停止。我很惊讶解析器不会将此报告为错误。