Hello all, I'm newbie in iPhone Application Programming. I can't get value from XML file and display it in UITableView. I need to get the name value of animal. How is the simple way to parse XML without attribute? I've been read NSXMLParser Documentation but the data is not displayed in my UITableView. Here is my XML file :
<?xml version="1.0" encoding="UTF-8"?>
<animals>
<animal>
<id>1</id>
<name>Elephant</name>
</animal>
<animal>
<id>2</id>
<name>Tiger</name>
</animal>
<animal>
<id>3</id>
<name>Bird</name>
</animal>
</animals>
I follow some tutorial about Parsing XML with attribute, but in my XML file, I don't have attribute. And this is my code to parse XML:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict {
if([elementName isEqualToString:@"animals"]) {
appDelegate.animals = [[NSMutableArray alloc] init];
}
else if([elementName isEqualToString:@"animal"]) {
}
else if([elementName isEqualToString:@"id"]){
aAnimal = [[Animal alloc] init];
aAnimal.animalID = elementName;
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if(!currentElementValue)
currentElementValue = [[NSMutableString alloc] initWithString:string];
else
[currentElementValue appendString:string];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if([elementName isEqualToString:@"animals"])
return;
else if([elementName isEqualToString:@"animal"]) {
}
else if([elementName isEqualToString:@"id"]) {
[appDelegate.animals addObject:aAnimal];
[aAnimal release];
aAnimal = nil;
}
else
[aAnimal setValue:currentElementValue forKey:elementName];
[currentElementValue release];
currentElementValue = nil;
}