2013-10-04 52 views
1

我只需要解析Trivia標記之間存在的元素。我不需要SAndWin之間的元素。任何幫助表示讚賞。提前致謝。僅在使用NSXMLParser的特定標記內解析元素

在下面的示例格式我只需要從小事標籤解析得分,用戶名

示例XML格式:

<Top10Score> 
<SAndWin> 
    <Member> 
    <SCORE>278</SCORE> 
    <MEM_ID>10</MEM_ID> 
    <EMAIL>[email protected]</EMAIL> 
    <USERNAME>prabu govind</USERNAME> 
    </Member> 
    <Member> 
    <SCORE>278</SCORE> 
    <MEM_ID>10</MEM_ID> 
    <EMAIL>[email protected]</EMAIL> 
    <USERNAME>prabu govind</USERNAME> 
    </Member> 
    <Member> 
    <SCORE>278</SCORE> 
    <MEM_ID>10</MEM_ID> 
    <EMAIL>[email protected]</EMAIL> 
    <USERNAME>Name1</USERNAME> 
    </Member> </SAndWin> 
    <Member> 
    <SCORE>278</SCORE> 
    <MEM_ID>10</MEM_ID> 
    <EMAIL>[email protected]</EMAIL> 
    <USERNAME>Name2</USERNAME> 
    </Member> 
    <Member> 
    <SCORE>278</SCORE> 
    <MEM_ID>10</MEM_ID> 
    <EMAIL>[email protected]</EMAIL> 
    <USERNAME>Name3</USERNAME> 
    </Member> 
<Trivia> 
    <Member> 
    <SCORE>278</SCORE> 
    <MEM_ID>44</MEM_ID> 
    <EMAIL>[email protected]</EMAIL> 
    <USERNAME>prabu govind</USERNAME> 
    </Member> 
    <Member> 
    <SCORE>278</SCORE> 
    <MEM_ID>48</MEM_ID> 
    <EMAIL>[email protected]</EMAIL> 
    <USERNAME>prabu govind</USERNAME> 
    </Member> 
    <Member> 
    <SCORE>278</SCORE> 
    <MEM_ID>22</MEM_ID> 
    <EMAIL>[email protected]</EMAIL> 
    <USERNAME>Name8</USERNAME> 
    </Member> </SAndWin> 
    <Member> 
    <SCORE>278</SCORE> 
    <MEM_ID>19</MEM_ID> 
    <EMAIL>[email protected]</EMAIL> 
    <USERNAME>Name6</USERNAME> 
    </Member> 
    <Member> 
    <SCORE>278</SCORE> 
    <MEM_ID>12</MEM_ID> 
    <EMAIL>[email protected]</EMAIL> 
    <USERNAME>Name4</USERNAME> 
    </Member> 
</Trivia> 
</Top10Score> 

回答

1

NSXMLParserSAX解析器。所以很難實現你的要求(不是很難,你需要一個標誌)。

你可以這樣做:

在喜歡你的.h文件中聲明一個BOOL:

BOOL標誌;

並實行解析方法,如:

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict 
{ 
    flag = false; 
    if ([elementName isEqualToString:@"Trivia"]) 
    { 
     flag = true; 
    } 
} 

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName 
{ 
    if ([elementName isEqualToString:@"Member"] && flag) 
    { 
     //read the value here 
    } 
} 

對於做類似的任務,最好使用DOM解析器GData XMLParser(我建議DOM解析器此要求)

,如果你正在使用GDataXMLParser,你可以這樣做:

- (void)parseMyXML:(NSString *)xml 
{ 
     GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:[xml dataUsingEncoding:NSUTF8StringEncoding] options:0 error:&error]; 
    if (doc == nil) { return nil; } 

    NSArray *members = [doc nodesForXPath:@"//Top10Score/Trivia" error:nil]; 
    for (GDataXMLElement *member in members) 
    { 
     int count = 0; 
     NSArray *names = [member elementsForName:@"Member"]; 
     while (names.count >count) 
     { 
      GDataXMLElement *firstName = (GDataXMLElement *) [names objectAtIndex:0]; 
      NSLog(@"Value : %@",[[names objectAtIndex:0] stringValue]); 
      //store data here 
      count++; 
     } 
    } 
} 
+0

我使用了標籤,它工作正常..感謝您的回答。 – Gowtham

+0

@Gowtham:高興:) –

0

需要初始化只有當你發現了一個花絮標籤的陣列。並在結束成員標記後將元素添加到數組中。

您必須聲明XMLPARSER,AR1,DIC1和curElement在.H文件

-(void)requestXMLParsing 
    { 

     NSURL *url = [NSURL URLWithString:URL]; 
     xmlParser = [[NSXMLParser alloc]initWithContentsOfURL:url]; 
     xmlParser.delegate = self; 
     BOOL success = [xmlParser parse]; 
     if (!success) { 

     } else { 

     } 
    } 

的#pragma馬克 - 的NSXMLParser委託

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict 
{ 
    if ([elementName isEqualToString:@"Trivia"]) { 
     ar1 = [[NSMutableArray alloc]init]; 
    } else if ([elementName isEqualToString:@"Member"]) { 
     dic1 = [[NSMutableDictionary alloc]init]; 
    } 

} 

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string 
{ 
    curElement = string; 

} 

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName 
{ 
    if ([elementName isEqualToString:@"Member"]) { 
     [ar1 addObject:dic1]; 
    } else if ([elementName isEqualToString:@"Trivia"]) { 
     NSLog(@"%@",ar1); 
    } else { 
     [dic1 setObject:curElement forKey:elementName]; 
    } 
} 

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError 
{ 

} 
+0

成員元素具有子標籤。我需要解析這些標籤..你能否提出更多更多 – Gowtham

+0

@Gowtham:請參閱我編輯的答案 – manujmv