views:

661

answers:

2

Hey All,

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.

Thanks & Regards

+2  A: 

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];
    }
}
Lachlan Roche
Of course, you would need to use NSXMLDocument to get NSXMLNodes (including NSXMLElements). NSXMLParser, which is what the questioner is using, will not give you any such objects.
Peter Hosey
A: 

Can somebody help me that how can i parse the attribute inside the tag [using NSXMLParser].

In your parser delegate, implement the parser:didStartElement:namespaceURI:qualifiedName:attributes: method. The attributes will be in the dictionary that the parser gives you in that last argument.

Peter Hosey