NSXMLParser 只读取一半的属性
我只得到前五个值,其余的返回为空。
这是 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)"
我在这里做错了什么吗?
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:(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]];
}
}
}
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这不是有效的 XML,属性之间不应该存在逗号,看看解析器中的值在到达第一个逗号时如何停止。我很惊讶解析器没有将此报告为错误。
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.
您的
AtlasEntry
项不是有效的 XML。属性之间不能有逗号;这会导致解析错误,这就是您返回空值的原因。例如,
应该
修复所有
AtlasEntry
项目的问题,然后您就可以开始了。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,
should be
Fix that for all your
AtlasEntry
items and you should be good to go.