chsab420
chsab420

Reputation: 191

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.

Upvotes: 0

Views: 2318

Answers (2)

Peter Hosey
Peter Hosey

Reputation: 96323

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.

Upvotes: 1

Lachlan Roche
Lachlan Roche

Reputation: 25946

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];
    }
}

Upvotes: 2

Related Questions