NSXMLParser only reading half the attributes
I'm only getting the first five values, the rest are coming back as null.
Here's the 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>
Here's the code:
- (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:(NS开发者_StackOverflowString *)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]];
}
}
}
And here's my log (NSMutableDictionary
cards):
"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)"
Am I doing something wrong here?
<AtlasEntry id="card1" x="0" y="950.0" w="180" h="250", a="2", d="3", name="Teresa Voldheart", team="horde" />
This is not valid XML, the comma's between attributes should not be there, see how the values from the parser stop when the first comma is reached. I'm surprised that the parser doesn't report this as an error.
Your AtlasEntry
items are not valid XML. You can't have commas between your attributes; that is resulting in a parse error which is why you are getting nulls back.
For example,
<AtlasEntry id="card16" x="1470.0" y="694.0" w="180" h="250", a="3", d="3", name="'Posion Tongue' McGillicutty", team="horde" />
should be
<AtlasEntry id="card16" x="1470.0" y="694.0" w="180" h="250" a="3" d="3" name="'Posion Tongue' McGillicutty" team="horde" />
Fix that for all your AtlasEntry
items and you should be good to go.
精彩评论