iPhone how to parse nested xml file and display in tableview
I have the following xml file
<Root>
<propertytype value="Property1">
<property>
<Property_Name>Name</Property_Name>
</defaultValue>
</property>
<property>
<Property_Name>Address</Property_Name>
</defaultValue>
</property>
<property>
<Property_Name>Age</Property_Name>
</defaultValue>
</property>
</propertytype>
<propertytype value="Property2">
<property>
<Property_Name>Cell Number</Property_Name>
</defaultValue>
</property>
<property>
<Property_Name>E-mail</Property_Name>
</defaultValue>
</property>
</propertytype>
</Root>
What i need is propertytype attributes value need to display in tableview and whenever i click on that value say "Property1", the respective elements(Name,Address and Age) need to be displayed in other view
Please help me?
I used the NSXMLParser and code is pasted below
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict {
if([elementName isEqualToString:@"Root"]) {
//Initialize the array.
appDelegate.subtype = [[NSMutableArray alloc] init];
appDelegate.books = [[NSMutableArray alloc] init];
}
else if([elementName isEqualToString:@"propertytype"]) {
sub=[[Subexptype alloc]init];
//Extract the attribute here.
sub.bookID=[attributeDict objectForKey:@"value"];
NSLog(@"Reading id value :%@", sub.bookID);
}
else if([elementName isEqualToString:@"property"]) {
aBook = [[Book alloc] init];
}
NSLog(@"Processing Element: %@", elementName);
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if(!currentElementValue)
currentElementValue = [[NSMutableString alloc] initWithString:string];
else
[currentElementValue appendString:string];
NSLog(@"Processing Value: %@", currentElementValue);
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if([elementName isEqualToString:@"Root"])
return;
//There is nothing to do if we encounter the Books element here.
//If we encounter the Book element howevere, we want to add the book object to the array
// and release the object.
if([elementName isEqualToString:@"propertyty开发者_StackOverflow社区pe"]){
[appDelegate.subtype addObject:sub];
[sub release];
sub=nil;
}
else if([elementName isEqualToString:@"property"]){
[appDelegate.books addObject:aBook];
[aBook release];
aBook = nil;
}
else
[aBook setValue:currentElementValue forKey:elementName];
[currentElementValue release];
currentElementValue = nil;
}
Now, i can able to get attribute value like "Property1" and "Property2" in tableview and whenever i click on Property1 or Property2 i get all elements like Name,Address,Age,E-mail and Cell Number.
But i don't want this way, if i click on Property1 its respective elements like Name, Address and Age shuold be displayed and if i click on property2 its elements Cell Number and E-mail shuold be displayed
Please help me where i am wrong
You have to create a class that contains Name,Address and Age and after parsing your file, you have to add each object in a NSMutableArray. I idvise you for parsing your file to use TBXML library downloadable from http://www.tbxml.co.uk/TBXML/TBXML_Free.html Hoping find this useful
Use
XMLReader.h
This will return you a dictionary for your xml.and from there you can easily extract the required content.
NSDictionary *_xmlDictionary = [[NSDictionary alloc] initWithDictionary:[XMLReader dictionaryForXMLData:responseData error:&error]];
Check the XMLReader class from here
http://dcraziee.wordpress.com/2013/05/29/parsing-xml-in-objective-c/
精彩评论