NSXMLParser help


#1

I haven’t actually yet dived into chapter 25 mostly skimmed it through, but I tried to write an XML parser, but for some reason I am missing figures from the XML file,

I’ve written the following code:

[code]
// XMLReader.h
// TESTXML

#import <Foundation/Foundation.h>

@interface XMLReader : NSObject

-(void) loadDataFromPath:(NSURL*) url;

  • (NSString *)formulateRequestFor:(NSArray *)tickers;

@end[/code]

[code]
// XMLReader.m
// TESTXML

#import “XMLReader.h”

#define QUOTE_QUERY_PREFIX @“http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(”
#define QUOTE_QUERY_SUFFIX @")&env=store://datatables.org/alltableswithkeys"

@implementation XMLReader

-(void) loadDataFromPath:(NSURL*) url;
{
NSData* data = [NSData dataWithContentsOfURL:url];
NSXMLParser* parser = [[NSXMLParser alloc] initWithData: data];

[parser setDelegate:self];
[parser parse];

}

  • (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict{

    NSLog(@"%@", elementName);
    NSLog(@"%@", attributeDict);
    NSLog(@"%@", [attributeDict valueForKey:@“Ask”]);
    }

  • (NSString *)formulateRequestFor:(NSArray *)tickers
    {
    NSMutableString *query = [[NSMutableString alloc] init];
    [query appendString:QUOTE_QUERY_PREFIX];
    for (int i = 0; i < [tickers count]; i++)
    {
    NSString *ticker = [tickers objectAtIndex:i];
    [query appendFormat:@"%%22%@%%22", ticker];
    if (i != [tickers count] - 1)
    {
    [query appendString:@"%2C"];
    }
    }
    [query appendString:QUOTE_QUERY_SUFFIX];

    return query;
    }

@end[/code]

This is the output I get

[quote]2012-06-13 18:13:05.404 TESTXML[49749:403] query
2012-06-13 18:13:05.409 TESTXML[49749:403] {
“xmlns:yahoo” = “http://www.yahooapis.com/v1/base.rng”;
“yahoo:count” = 1;
“yahoo:created” = “2012-06-13T16:12:47Z”;
“yahoo:lang” = “en-US”;
}
2012-06-13 18:13:05.412 TESTXML[49749:403] (null)
2012-06-13 18:13:05.413 TESTXML[49749:403] results
2012-06-13 18:13:05.414 TESTXML[49749:403] {
}
2012-06-13 18:13:05.415 TESTXML[49749:403] (null)
2012-06-13 18:13:05.416 TESTXML[49749:403] quote
2012-06-13 18:13:05.419 TESTXML[49749:403] {
symbol = “BOL.ST”;
}
2012-06-13 18:13:05.420 TESTXML[49749:403] (null)
2012-06-13 18:13:05.420 TESTXML[49749:403] Ask
2012-06-13 18:13:05.422 TESTXML[49749:403] {
}
2012-06-13 18:13:05.423 TESTXML[49749:403] (null)
.
.
.
2012-06-13 18:13:05.940 TESTXML[49749:403] DividendYield
2012-06-13 18:13:05.941 TESTXML[49749:403] {
}
2012-06-13 18:13:05.941 TESTXML[49749:403] (null)
2012-06-13 18:13:05.941 TESTXML[49749:403] PercentChange
2012-06-13 18:13:05.941 TESTXML[49749:403] {
}
2012-06-13 18:13:05.942 TESTXML[49749:403] (null)[/quote]

This is the link to the xml file>

http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22BOL.ST%22)&env=store://datatables.org/alltableswithkeys

If anyone can see what is missing help would be greatly appreciated!


#2

If I look at your output, you get all the names for the start of the elements (like <query …>, , , etc). So (part of) the code you’ve written for parsing those starting elements works. What I am missing is code that saves the characters that come after the parser has found the start of an element (like 90.30 after the start of the element ). I suggest to read chapter 25 more closely to see how BNR does this, in stead of skimming through, because I think that it will clarify a lot!

Edit: it also looks like you are confusing attributes with elements, because you are trying to find the value for attribute Ask in the dictionary (see your third NSLog). Since Ask is an element in the XML file, and not an attribute anywhere, it is not in the dictionary and this produces the null values in your output.