How to parse the xml file which include attributes in iphone
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"])
{
NSMut开发者_Python百科ableArray *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
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).
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.
精彩评论