Search code examples
objective-ccocoansxmlxml-attribute

How to Parse through nsXml Parser


i am very new to iphone Development and i am asked to use nsxml parser to parse xml from a google api. i have parsed another url which has xml but i am not able to parse google's because it is using id's to store data rather than inside tag. i.e.

<forecast_information>
    <city data="Anaheim, CA"/>
    <postal_code data="anaheim,ca"/>
    <latitude_e6 data=""/>
    <longitude_e6 data=""/>
    <forecast_date data="2010-03-11"/>
    <current_date_time data="2010-03-12 07:06:32 +0000"/>
    <unit_system data="US"/>
</forecast_information>

Can somebody help me that how can i parse the attribute inside the tag.


Solution

  • The name and value of a NSXMLNode are given by methods name and stringValue respectively. For an attribute node, these are the attibute name and value.

    The attributes of a NSXMLElement are given by method attributes, or a particular attribute can be accessed by name with method attributeForName:.

    NSXMLElement *forecast_information;
    
    for( NSXMLElement *el in [forecast_information children] ) {
        NSString *name = [el name];
        NSString *value = @"";
        if ([el attributeForName: @"data"]) {
          value = [[el attributeForName: @"data"] stringValue];
        }
    }