使用NSXMLParser仅在特定标记内解析元素

时间:2013-10-04 05:31:00

标签: ios nsxmlparser

我只需要解析Trivia标签之间存在的元素。我不需要SAndWin之间的元素。任何帮助表示赞赏。提前谢谢。

在下面的示例格式中,我需要解析分数,仅来自Trivia标签的用户名

示例XML格式:

<Top10Score>
 <SAndWin>
   <Member>
     <SCORE>278</SCORE>
     <MEM_ID>10</MEM_ID>
     <EMAIL>ggg10@gmail.com</EMAIL>
     <USERNAME>prabu govind</USERNAME>
   </Member>
   <Member>
     <SCORE>278</SCORE>
     <MEM_ID>10</MEM_ID>
     <EMAIL>ggg10@gmail.com</EMAIL>
     <USERNAME>prabu govind</USERNAME>
   </Member> 
   <Member>
     <SCORE>278</SCORE>
     <MEM_ID>10</MEM_ID>
     <EMAIL>c1@gmail.com</EMAIL>
     <USERNAME>Name1</USERNAME>
   </Member>    </SAndWin>
   <Member>
     <SCORE>278</SCORE>
     <MEM_ID>10</MEM_ID>
     <EMAIL>a1@gmail.com</EMAIL>
     <USERNAME>Name2</USERNAME>
   </Member>
   <Member>
     <SCORE>278</SCORE>
     <MEM_ID>10</MEM_ID>
     <EMAIL>b2@gmail.com</EMAIL>
     <USERNAME>Name3</USERNAME>
   </Member>
<Trivia>
   <Member>
     <SCORE>278</SCORE>
     <MEM_ID>44</MEM_ID>
     <EMAIL>ggg10@gmail.com</EMAIL>
     <USERNAME>prabu govind</USERNAME>
   </Member>
   <Member>
     <SCORE>278</SCORE>
     <MEM_ID>48</MEM_ID>
     <EMAIL>ggg10@gmail.com</EMAIL>
     <USERNAME>prabu govind</USERNAME>
   </Member> 
   <Member>
     <SCORE>278</SCORE>
     <MEM_ID>22</MEM_ID>
     <EMAIL>cname@gmail.com</EMAIL>
     <USERNAME>Name8</USERNAME>
   </Member>    </SAndWin>
   <Member>
     <SCORE>278</SCORE>
     <MEM_ID>19</MEM_ID>
     <EMAIL>cname1@gmail.com</EMAIL>
     <USERNAME>Name6</USERNAME>
   </Member>
   <Member>
     <SCORE>278</SCORE>
     <MEM_ID>12</MEM_ID>
     <EMAIL>ename@gmail.com</EMAIL>
     <USERNAME>Name4</USERNAME>
   </Member>
</Trivia>
</Top10Score>

2 个答案:

答案 0 :(得分:1)

NSXMLParser SAX Parser 。所以实现你的要求非常困难(你需要一个标志并不那么难)。

你可以这样做:

在你的.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
    }
}

为了执行类似的任务,最好使用像GData XMLParser这样的 DOM Parser (我推荐DOM Parsers用于此要求)

如果你正在使用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++;
        }
     }
}

答案 1 :(得分:0)

只有在找到Trivia标记时才需要初始化数组。并在结束Member标记后向元素添加元素。

您必须在 .h 文件中声明xmlParser,ar1,dic1和curElement

-(void)requestXMLParsing
    {

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

        } else  {

        }
    } 

#pragma mark - 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
{

}