开发者

Need to get a attribute from an XML file

I'm going to cut straight to the question, I have a XML file that has attributes like for example:

<Lowest units="p">135.9</Lowest>

What I need to get is the value from 'units', as if I just get the value from 'Lowest' it displays it as ' { Lowest = "135.9"\n\t; } ' and I'm assuming if I got it from the attribute it would just display '135.9' instead of above.

Here's the main two bits of coding I'm using:

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSStr开发者_如何学运维ing *)qName attributes:(NSDictionary *)attributeDict
{
    NSLog(@"found this element: %@", elementName);

    currentElement = [elementName copy];


    if ([elementName isEqualToString:@"Fuel"])
    {
        item = [[NSMutableDictionary alloc] init];
        fuel_price = [[NSMutableString alloc] init];
        fuel_type = [[NSMutableString alloc] init];
    }


}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName 
{
    NSLog(@"ended element: %@", elementName);

    if ([elementName isEqualToString:@"Fuel"])
    {
        [item setObject:fuel_price forKey:@"p"];

        [item setObject:fuel_type forKey:@"type"];

        [prices addObject:[item copy]];

        NSLog(@"adding fuel prices: %@ - %@", fuel_price, fuel_type);
    }

}

Hopefully someone can point me in the right direction, as I've tried so many methods with no luck.

Thanks in advance! :)


All the attributes are stored inside this attributeDict NSDcitionary you get in didStartElement... method and it's formatted so that attribute name is the key and value is, well, value :) So inside your element if you want to get the value of attribute named units you would do it this way:

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    if ([elementName isEqualToString:@"Lowest"]) {
        NSString *units = [attributeDict objectForKey:@"units"];
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜