2013-02-25 97 views
1

我正在解析下面的XML與NSXMLParser,但希望僅根據XML中找到的唯一元素解析某些元素和屬性,然後中止解析。NSXMLParser:解析基於另一個元素的指定元素

例如,我的XML看起來是這樣的:

<?xml version="1.0" encoding="UTF-8"?> 
<systems> 
<system id="system1"> 
    <game title="game1"> 
     <checksums> 
      <checksum value="checksumvalue1" /> 
     </checksums> 
     <players> 
      <player name="name1" description="description1" /> 
      <player name="name2" description="description2" /> 
     </players> 
    </game> 
</system> 

我希望能夠返回game所有players只是對checksum 值基於所以,如果我通過checksumvalue1到我的方法,我想只返回在那個game節點中發現的players與唯一的校驗和。在XML中,將會有其他帶有校驗和的遊戲,但校驗和總是唯一的。

到目前爲止,我有一些基本的解析器代碼,只是從校驗和值中讀取元素和屬性,但我不知道該從哪裏去。我也想知道XPath的NSXMLNode是否更有意義。

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict{ 

if ([elementName isEqualToString:@"checksum"] && [[attributeDict valueForKey:@"value"] isEqualToString:@"checksumvalue1"]) { 
    NSString *checksumValue = [attributeDict valueForKey:@"value"]; 
    NSLog(@"value: %@", checksumValue); 

// print players in only one game node that contains the unique checksum value 
    } 
} 

感謝您的幫助!

回答

0

你必須跟蹤你是否已經找到了校驗和以及你所遇到的所有玩家。您需要布爾型foundGameChecksumNSMutableArray* players。不要忘了players = [[NSMutableArray alloc] init];

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict{ 

    if ([elementName isEqualToString:@"checksum"] && [[attributeDict valueForKey:@"value"] isEqualToString:@"checksumvalue1"]) { 
     NSString *checksumValue = [attributeDict valueForKey:@"value"]; 
     NSLog(@"value: %@", checksumValue); 

     foundGameChecksum = YES; 
     // print players in only one game node that contains the unique checksum value 
    } else if(foundGameChecksum && [elementName isEqualToString:@"player"]) { 
     [players addObject:[attributeDict valueForKey:@"name"]]; 
    } 
} 

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName 
{ 
    if(foundGameChecksum && [elementName isEqualToString:@"players"]) { 
     [parser abortParsing]; // Stop parsing 
    } 
} 

執行[parser parse];然後你就會有玩家在players

即:

<?xml version="1.0" encoding="UTF-8"?> 
<systems> 
    <system id="system1"> 
     <game title="game1"> 
      <checksums> 
       <checksum value="checksumvalue1" /> <!-- Found. Start adding --> 
      </checksums> 
      <players> 
       <player name="name1" description="description1" /> <!-- Add --> 
       <player name="name2" description="description2" /> <!-- Add --> 
      </players> <!-- Stop adding --> 
     </game> 
    </system> 
</systems> 
+0

完美!謝謝 – brymaster 2013-02-25 08:04:55

+0

有一件事,是否有任何我需要放棄的地方,在它已經找到所有玩家之後繼續下去?看起來它仍然會解析整個XML,即使它找到了播放器並將它們全部添加到數組中。 – brymaster 2013-02-25 08:33:22

+0

找到[另一個問題]的答案(http://stackoverflow.com/questions/6340731/easy-way-to-cancel-current-nsxmlparser-parsing-operation)'[解析器abortParsing];'我編輯了我的答案。 – Ric 2013-02-25 08:37:50

相關問題