1

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

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

示例 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>
4

2 回答 2

1

NSXMLParser是一个SAX 解析器。所以很难实现你的要求(你需要一个标志并不难)。

你可以这样做:

在您的 .h 文件中声明一个 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++;
        }
     }
}
于 2013-10-04T05:58:11.047 回答
0

只有在找到 Trivia 标记时才需要初始化数组。并在结束成员标签后将元素添加到数组中。

您必须在.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
{

}
于 2013-10-04T05:43:07.873 回答