Discussion in 'iOS Programming' started by MickeyT, Jan 12, 2013.

  1. MickeyT macrumors member

    Apr 26, 2010
    Newcastle, United Kingdom
    I have deviated from my book, which was a mistake.....

    I discovered a stock API from Google yesterday that returns various bits of information about a stock in what I think is XML. I set about reading up on the NSXMLParser class.

    I have had some success. The following code successfully prints the top level element names of what is returned, and then attempts to print the value associated with the element name:

    - (void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
        NSLog(@"%@", elementName);
        NSEnumerator *dictEnumerator = [attributeDict keyEnumerator];
        id selectedKey;
        if ([elementName isEqualToString:@"last"]) {
            while (selectedKey = [dictEnumerator nextObject]) {
               NSLog(@"%@", [attributeDict objectForKey:elementName]);
    However, the following just returns (null):

    NSLog(@"%@", [attributeDict objectForKey:elementName]);
    I was able to view what the API returned on my work laptop in internet explorer, but it doesn't seem to appear when I view in Safari at home. So, from memory, there are no further "levels" past the first tier of element names. "last" should return the last retrieved stock price; there isn't another list of elements below "last", only the actual value.

    For information, this is how the NSXMLParser object is initialised and set off doing it's thing:

    NSURL *url = [[NSURL alloc] initWithString:urlString];
        NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
        [parser setDelegate:self];
        BOOL parseSuccess = [parser parse];
        [parser setDelegate:nil];
    .. and urlString is, say, http://www.google.com/ig/api?stock=AAPL.

    Please could anyone explain why I cannot retrieve the stock price value from "last" (or indeed any of the other element names that I have tried this with)?

    Thank you
  2. KoolStar macrumors demi-god


    Oct 16, 2006
    As we can see it looks like your only looking at the first tag of the xml. The xml that is returned from the api looks like below:

    <xml_api_reply version="1">
    <finance module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0">
    <symbol data="AAPL"/>
    <pretty_symbol data="AAPL"/>
    <symbol_lookup_url data="/finance?client=ig&q=AAPL"/>
    <company data="Apple Inc."/>
    <exchange data="Nasdaq"/>
    <exchange_timezone data="ET"/>
    <exchange_utc_offset data="+05:00"/>
    <exchange_closing data="960"/>
    <divisor data="2"/>
    <currency data="USD"/>
    <last data="506.09"/>
    <high data="509.44"/>
    <low data="492.50"/>
    <volume data="22279"/>
    <avg_volume data="20901"/>
    <market_cap data="476074.81"/>
    <open data="494.64"/>
    <y_close data="485.92"/>
    <change data="+20.17"/>
    <perc_change data="4.15"/>
    <delay data="0"/>
    <trade_timestamp data="15 hours ago"/>
    <trade_date_utc data="20130116"/>
    <trade_time_utc data="210001"/>
    <current_date_utc data="20130117"/>
    <current_time_utc data="122156"/>
    <symbol_url data="/finance?client=ig&q=AAPL"/>
    <chart_url data="/finance/chart?q=NASDAQ:AAPL&tlf=12"/>
    <disclaimer_url data="/help/stock_disclaimer.html"/>
    <ecn_url data=""/>
    <isld_last data="510.50"/>
    <isld_trade_date_utc data="20130117"/>
    <isld_trade_time_utc data="120612"/>
    <brut_last data=""/>
    <brut_trade_date_utc data=""/>
    <brut_trade_time_utc data=""/>
    <daylight_savings data="false"/>
    We can see that the last tag is inside the Finance tag. Thus meaning that you must first process the finance tag and then start looping through the items inside the finance tag. I would recommend looking at the following tutorial and using the GDATA libraray from google.

    LINK: http://www.raywenderlich.com/725/how-to-read-and-write-xml-documents-with-gdataxml
  3. MickeyT thread starter macrumors member

    Apr 26, 2010
    Newcastle, United Kingdom
    Thank you. I will look into that and come back if anything doesn't make sense.
  4. moonman239 macrumors 68000

    Mar 27, 2009
    If I were you, I would create a subclass of NSObject that you can use to store all the information you need that pertains to a node.

Share This Page