I got a question with xml parsing. Normally,the XML file style like this:
<person>
<name> abc </name>
<age> 19 </age>
<gender> male </gender>
</person>
In this way, we use
elementName isEqualToString:@"name"
elementName isEqualToString:@"age"
elementName isEqualToString:@"gender"
to identify the element.
Now, I'm facing a xml file style like:
<person>
<element type="name">abc</element>
<element type="age">19</element>
<element type="gender">male</element>
</person>
In this situation, we can't use
elementName isEqualToString:@"name"
So I tried to use
if ([elementName isEqualToString:@"element"]) {
if ([[attributeDict objectForKey:@"type"] isEqualToString:@"name"){
.....
}
}
to identify the "name", but it gave me a none value, there is nothing if i tried to get the value of "name".
So, does anyone have any suggestions to fix this problem? thanks
PS: Here are part of my code
- (void)parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
attributes:(NSDictionary *)attributeDict {
if ( [elementName isEqualToString:@"area"]) {
if ([[attributeDict objectForKey:@"type"] isEqualToString:@"location"]) {
NSString *description = [attributeDict objectForKey:@"description"];
NSArray *lineEntry = [[NSArray alloc] initWithObjects:description,nil ];
NSArray *lineKeys = [[NSArray alloc] initWithObjects:@"description",nil];
NSDictionary *dictItem = [[NSDictionary alloc] initWithObjects:lineEntry forKeys:lineKeys];
[lineKeys release];
[lineEntry release];
[dataLoad addObject:dictItem];
[dictItem release];
}
}
if ([elementName isEqualToString:@"forecast-period"]) {
NSString *index = [attributeDict objectForKey:@"index"];
NSString *time = [attributeDict objectForKey:@"start-time-local"];
NSArray *indexEntry = [[NSArray alloc] initWithObjects:index,time,nil];
NSArray *indexKey = [[NSArray alloc] initWithObjects:@"index",@"start-time-local",nil];
NSDictionary *dicIndex = [[NSDictionary alloc] initWithObjects:indexEntry forKeys:indexKey];
[indexEntry release];
[indexKey release];
[weatherLoad addObject:dicIndex];
[dicIndex release];
}
}
it worked fine with those 2 parts, but it doesn't work if i try to get "forecast_icon_code","air_temperature_minimum","air_temperature_maximum","precis".
<forecast-period index="1" start-time-local="2010-11-19T00:00:00+11:00" end-time-local="2010-11-20T00:00:00+11:00" start-time-utc="2010-11-18T13:00:00Z" end-time-utc="2010-11-19T13:00:00Z">
<element type="forecast_icon_code">1</element>
<element type="air_temperature_minimum" units="Celsius">12</element>
<element type="air_temperature_maximum" units="Celsius">24</element>
<text type="precis">Sunny.</text>
</forecast-period>
Any suggestions? BTW, the whole xml file is here: ftp://ftp2.bom.gov.au/anon/gen/fwo/IDV10753.xml