我有一个以下的xml文件。我想解析元素current_date_time
,condition
,humidity
,icon
,wind_condition
,low
,high
。所有这些元素都包含属性。我知道如何解析一个简单的xml文件。但所有这些元素都包含属性值。
那我该如何解析这个xml文件呢?
<?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"]];
}
请帮我解决这个问题。谢谢
答案 0 :(得分:5)
假设<postal_code data="" />
你需要数据值,所以你需要做的是:
-(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"]
将为您提供属性值,然后您可以添加任何位置(无论您使用的是什么结构)。
答案 1 :(得分:1)
您可以使用DDXML解析器并只使用此
[[Element attributeForName:@"url"] stringValue];
其中attributeForName是您可以使用的函数。 这个(DDXML)是一个非常好的libxml解析器。