2011-03-06 6 views
0

처음 5 개의 값만 가져오고 나머지는 null로 되돌아갑니다. 여기 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)" 

는 여기 뭔가 잘못하고 있는가가?

답변

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

이 유효하지 않습니다 XML, 속성 사이의 쉼표의 파서 정류장에서 값이 첫 번째 쉼표에 도달했을 때 방법을 참조 없을 것이다. 나는 파서가 이것을 오류로보고하지 않는다는 것에 놀랐다.

+1

duh. 내가 그랬는지 모르겠다. 당신은 무엇인가 보았고, 당신이 더 잘 압니다 만, 어쨌든 그것이 옳다고 생각합니다. 감사. – PruitIgoe

2

AtlasEntry 항목이 유효한 XML이 아닙니다. 속성 사이에는 쉼표를 사용할 수 없습니다. 그 결과 파스 (parse) 오류가 발생하여 널 (null)을 다시 얻게됩니다. 예를 들어

,

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

는 모든 AtlasEntry 항목에 대해 당신이 갈 수해야한다고

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

수정해야합니다.

관련 문제