0

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.

Rais Alam
  • 6,970
  • 12
  • 53
  • 84
chsab420
  • 191
  • 2
  • 3
  • 14

2 Answers2

2

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
  • 25,678
  • 5
  • 79
  • 77
  • 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 Mar 13 '10 at 18:56
1

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
  • 95,783
  • 15
  • 211
  • 370