1

I have an the following xml file. I want to parse the elements current_date_time, condition, humidity, icon, wind_condition, low, high. All these elements include attributes. I know how to parse a simple xml file. But all these elements contains attribute values.

So how would I parse this xml file?

    <?xml version="1.0" ?> 
    <xml_api_reply version="1">
        <weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0">
            <forecast_information>
                <city data="" /> 
                <postal_code data="" /> 
                <latitude_e6 data="50500000" /> 
                <longitude_e6 data="30500000" /> 
                <forecast_date data="2011-05-27" /> 
                <current_date_time data="2011-05-27 04:30:00 +0000" /> 
                <unit_system data="US" /> 
            </forecast_information>
            <current_conditions>
                <condition data="Clear" /> 
                <temp_f data="57" /> 
                <temp_c data="14" /> 
                <humidity data="Humidity: 59%" /> 
                <icon data="http://g0.gstatic.com/images/icons/onebox/weather_sunny-40.gif" /> 
                <wind_condition data="Wind: S at 9 mph" /> 
            </current_conditions>
            <forecast_conditions>
                <day_of_week data="Fri" /> 
                <low data="54" /> 
                <high data="72" /> 
                <icon data="http://g0.gstatic.com/images/icons/onebox/weather_partlycloudy-40.gif" /> 
                <condition data="Mostly Sunny" /> 
            </forecast_conditions>
            <forecast_conditions>
                <day_of_week data="Sat" /> 
                <low data="52" /> 
                <high data="79" /> 
                <icon data="http://g0.gstatic.com/images/icons/onebox/weather_partlycloudy-40.gif" /> 
                <condition data="Mostly Sunny" /> 
            </forecast_conditions>
            <forecast_conditions>
                <day_of_week data="Sun" /> 
                <low data="55" /> 
                <high data="84" /> 
                <icon data="http://g0.gstatic.com/images/icons/onebox/weather_partlycloudy-40.gif" /> 
                <condition data="Mostly Sunny" /> 
            </forecast_conditions>
            <forecast_conditions>
                <day_of_week data="Mon" /> 
                <low data="63" /> 
                <high data="84" /> 
                <icon data="http://g0.gstatic.com/images/icons/onebox/weather_partlycloudy-40.gif" /> 
                <condition data="Mostly Sunny" /> 
            </forecast_conditions>
        </weather>
    </xml_api_reply>


    Please help me in solving this problem. Thanks. Can anybody provide me any sample code on this?


i have done this code to parse the above xml file but my problem is checks only the first xml tag i.e the root element xml_ap_reply for every checking

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString*) elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString*)qualifiedName attributes:(NSDictionary*)attributeDict
{


if ([elementName isEqualToString:@"xml_api_reply"])
    {
        mWeather = [[TWeatherElement alloc]init];


    }

     if([elementName isEqualToString:@"current_date_time"])
    {
         NSMutableArray *temp=[[NSMutableArray alloc]init];
        self.mParserArray=temp;
        [temp release];


         [mParserArray addObject:[attributeDict valueForKey:@"data"]];

    }

     if([elementName isEqualToString:@"condition"])
    {
        NSMutableArray *temp=[[NSMutableArray alloc]init];
        self.mParserArray=temp;
        [temp release];


        [mParserArray addObject:[attributeDict valueForKey:@"data"]];

    }

     if([elementName isEqualToString:@"humidity"])
    {
        NSMutableArray *temp=[[NSMutableArray alloc]init];
        self.mParserArray=temp;
        [temp release];


        [mParserArray addObject:[attributeDict valueForKey:@"data"]];

    }

     if([elementName isEqualToString:@"icon"])
    {
        NSMutableArray *temp=[[NSMutableArray alloc]init];
        self.mParserArray=temp;
        [temp release];


        [mParserArray addObject:[attributeDict valueForKey:@"data"]];

    }   
     if([elementName isEqualToString:@"wind_condition"])
    {
        NSMutableArray *temp=[[NSMutableArray alloc]init];
        self.mParserArray=temp;
        [temp release];


        [mParserArray addObject:[attributeDict valueForKey:@"data"]];

    }

     if([elementName isEqualToString:@"low"])
    {
        NSMutableArray *temp=[[NSMutableArray alloc]init];
        self.mParserArray=temp;
        [temp release];


        [mParserArray addObject:[attributeDict valueForKey:@"data"]];

    }

     if([elementName isEqualToString:@"high"])
    {
        NSMutableArray *temp=[[NSMutableArray alloc]init];
        self.mParserArray=temp;
        [temp release];


        [mParserArray addObject:[attributeDict valueForKey:@"data"]];

    }

please help me in solving this problem.Thanks

antyrat
  • 27,479
  • 9
  • 75
  • 76
Rocky
  • 1,427
  • 1
  • 28
  • 65
  • Here is a good tutorial that shows XML parsing using GDataXMLParser. [how-to-read-and-write-xml-documents-with-gdataxml](http://www.raywenderlich.com/725/how-to-read-and-write-xml-documents-with-gdataxml) – ArunGJ May 27 '11 at 05:46
  • Duplicate of http://stackoverflow.com/questions/1519433/iphone-parse-xml-file-with-attribute or http://stackoverflow.com/questions/2040028/parsing-xml-details-and-attributes-iphone – Viktor Apoyan May 27 '11 at 05:48

2 Answers2

5

suppose <postal_code data="" /> and you want data value so what you have to do is:

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{         

    if([elementName isEqualToString:@"postal_code"])
    {
        NSMutableArray *temp = [[NSMutableArray alloc] init];
        self.registrationResponseArray = temp;
        [temp release];

        [registrationResponseArray addObject:[attributeDict valueForKey:@"data"]];  
    }
}

[attributeDict valueForKey:@"data"] will give you the attribute value, then you can add anywhere (whatever structure you are using).

rog
  • 5,351
  • 5
  • 33
  • 40
Gypsa
  • 11,230
  • 6
  • 44
  • 82
1

You can use DDXML parser and simply use this

[[Element attributeForName:@"url"] stringValue];

where attributeForName is a function available to you. This(DDXML) is a realy nice libxml parser.

Praveen S
  • 10,355
  • 2
  • 43
  • 69
Suny
  • 1,175
  • 1
  • 10
  • 27