Need help with XMLParser

Discussion in 'iOS Programming' started by benne90, Jun 14, 2012.

  1. benne90 macrumors newbie

    Joined:
    Jun 14, 2012
    #1
    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

    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!
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    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.
     
  3. benne90 thread starter macrumors newbie

    Joined:
    Jun 14, 2012
    #3
    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.
     

Share This Page