【问题标题】:NSXMLParser only reading half the attributesNSXMLParser 只读取一半的属性
【发布时间】:2011-07-09 15:24:05
【问题描述】:

我只得到前五个值,其余的返回为空。

这是 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]];
    }

    }
}

这是我的日志(NSMutableDictionarycards):

"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)"

我在这里做错了吗?

【问题讨论】:

    标签: objective-c cocoa nsxmlparser


    【解决方案1】:
    <AtlasEntry id="card1" x="0" y="950.0" w="180" h="250", a="2", d="3", name="Teresa Voldheart", team="horde" />
    

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

    【讨论】:

    • 呃。我不知道我是这样做的。你曾经看过某样东西,只是出于某种原因认为它是正确的,即使你知道得更好。谢谢。
    【解决方案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 项目修复该问题,您应该一切顺利。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-22
      • 1970-01-01
      • 2010-11-22
      • 2014-08-22
      • 2015-02-01
      • 2011-01-28
      • 1970-01-01
      相关资源
      最近更新 更多