1

我在获取 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);
}
4

1 回答 1

0

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

/History/master/historypoints/@win

我对ios平台了解不多,所以需要在ios上找一些支持xpath的xml库。这是一个在线 xpath 测试器,可以使用 xpath 表达式。

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

于 2013-09-06T07:09:16.270 回答