Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

benne90

macrumors newbie
Original poster
Jun 14, 2012
2
0
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:

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!
 
Please describe what you expected to happen. We can see the output, so we know what happened, but you never described what you expected. To me, the output is what I expected, given the XML and the posted. What you expected is unknown to me.


Your delegate only has code for one method: the parser:didStartElement: method. When the parser starts an element, there is no element value yet. An element's value comes after the element is started, i.e. after the start tag.

Example: in this xml, the red hilites the start of each element, i.e. its start tag.
Code:
[COLOR="Red"]<results>[/COLOR]
 [COLOR="red"]<quote symbol="BOL.ST"[/COLOR]>
  [COLOR="red"]<Ask>[/COLOR]91.50</Ask>
  [COLOR="red"]<AverageDailyVolume>[/COLOR]3272404</AverageDailyVolume>
  [COLOR="red"]<Bid>[/COLOR]91.40</Bid>

You will need to study the NSXMLParser reference docs more, and look at more working examples of when and where parsed data appears, and in exactly which delegate methods.

Also, the value of the Ask element is never going to appear in an attributeDict, because an element's value is not an attribute. If you don't know the difference between attributes and element data, you should study the fundamentals of XML more, so you fully understand the terms.

If you're learning from a book or tutorial, exactly which one? Title, author, edition? URL?


It would also be good to label your output lines, like so:
Code:
    NSLog(@"el: %@", elementName);
    NSLog(@"attrs: %@", attributeDict);
    NSLog(@"%@", [attributeDict valueForKey:@"Ask"]);
It doesn't need to be exactly like that. It just needs something more than undifferentiated @"%@" for everything.
 
My knowledge about xml files is limited, I just recently tried to interpret them, but you answered my question. I expected values to come up but now I understand why it didn't thanks for the elaborate reply. I will look into the documentation :)

Oh and to answer your question about the book, I did this by looking into the documentation and looking around the web. While I am also reading Big Nerd Ranch 3rd, but this is just a separate project.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.