如何在一个数组中获取多个属性值

时间:2013-09-06 06:36:18

标签: iphone ios xml

我在获取xml响应中的属性值时遇到问题

XML

<History>
<master ids=\"1\" >
    <historypoints id=\"1\" bankerpoint=\"9\" playerpoint=\"0\" win=\"1\" playerpair=\"0\" bankerpair=\"0\" />
</master>
<master ids=\"2\" >
    <historypoints id=\"2\" bankerpoint=\"5\" playerpoint=\"7\" win=\"0\" playerpair=\"0\" bankerpair=\"0\" />
    <historypoints id=\"3\" bankerpoint=\"6\" playerpoint=\"7\" win=\"0\" playerpair=\"0\" bankerpair=\"0\" />
    <historypoints id=\"4\" bankerpoint=\"0\" playerpoint=\"4\" win=\"0\" playerpair=\"0\" bankerpair=\"0\" />
</master>
<master ids=\"3\" >
    <historypoints id=\"5\" bankerpoint=\"5\" playerpoint=\"3\" win=\"1\" playerpair=\"0\" bankerpair=\"0\" />
    <historypoints id=\"6\" bankerpoint=\"6\" playerpoint=\"0\" win=\"1\" playerpair=\"0\" bankerpair=\"0\" />
    <historypoints id=\"7\" bankerpoint=\"9\" playerpoint=\"0\" win=\"1\" playerpair=\"1\" bankerpair=\"0\" />
    <historypoints id=\"8\" bankerpoint=\"6\" playerpoint=\"0\" win=\"1\" playerpair=\"0\" bankerpair=\"0\" />
</master>
<master ids=\"4\" >
    <historypoints id=\"9\" bankerpoint=\"0\" playerpoint=\"8\" win=\"0\" playerpair=\"0\" bankerpair=\"0\" />
</master>

在上面的xml中我需要在一个数组中获得“win”属性值,任何人都可以请你帮我解决问题。

取消我的代码

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

    NSLog(@"elementName %@",elementName);
     NSLog(@"attributeDict %@",attributeDict);
    if ([elementName isEqualToString:@"History"]) {
        label = @"History";

    }
    else if ([elementName isEqualToString:@"historypoints"]) {
        label = @"historypoints";

        if ([attributeDict valueForKey:@"win"]) {
            NSLog(@"Log values is more useful ");
            label = @"win";
        }
    }
}



- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
    NSLog(@"foundCharacters %@",string);

    if ([label isEqualToString:@"win"]){

        self.typeStr = string;
        [typearr addObject:string];

    }
}

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

    NSLog(@"didEndElement elementName:%@",elementName);
}

1 个答案:

答案 0 :(得分:0)

您可以在此处使用XPath。这是解决你任务的表达方式。

/History/master/historypoints/@win

我对ios平台了解不多,所以你需要在ios上找到一些支持xpath的xml库。 这是一个online xpath tester来玩xpath表达式。

另请注意,您的xml缺少结束“历史记录”根标记。