I am trying to read xml files into the command line, but I can't seem to get the values of the elements, can someone give any pointers as how I can fix it?
I've written the following code:
This is the output I get
This is the link to the xml file>
http://query.yahooapis.com/v1/publi...&env=store://datatables.org/alltableswithkeys
If anyone can see what is missing help would be greatly appreciated!
I've written the following code:
Code:
//
// main.m
// TESTXML
#import <Foundation/Foundation.h>
#import "XMLReader.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSArray *array = [NSArray arrayWithObject:@"BOL.ST"];
XMLReader *reader = [[XMLReader alloc] init];
NSString *stringURL = [reader formulateRequestFor:array];
NSLog(@"%@",stringURL);
[reader loadDataFromPath:[NSURL URLWithString:stringURL]];
}
return 0;
}
Code:
// XMLReader.h
// TESTXML
#import <Foundation/Foundation.h>
@interface XMLReader : NSObject <NSXMLParserDelegate>
-(void) loadDataFromPath:(NSURL*) url;
- (NSString *)formulateRequestFor:(NSArray *)tickers;
@end
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
This is the output I get
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)
This is the link to the xml file>
http://query.yahooapis.com/v1/publi...&env=store://datatables.org/alltableswithkeys
If anyone can see what is missing help would be greatly appreciated!