parssing is going well. but i a not able to get value in ‘aBook’ object of class ‘Book'(whereas aBook have all the property of Book class like ‘id’,’iid’,’catname’,’subcatname’,’url’i.e declared and retained ). what to do get values in aBook?
here is output in gdb :
2011-04-28 15:08:12.046 XML[7053:20b] Processing Element: Table
2011-04-28 15:08:12.047 XML[7053:20b] Processing Value:
2011-04-28 15:08:12.048 XML[7053:20b] Processing Element: id
2011-04-28 15:08:12.048 XML[7053:20b] Processing Value:
1
2011-04-28 15:08:12.049 XML[7053:20b] Processing Value:
2011-04-28 15:08:12.050 XML[7053:20b] Processing Element: catname
2011-04-28 15:08:12.050 XML[7053:20b] Processing Value:
Birthday
2011-04-28 15:08:12.051 XML[7053:20b] Processing Value:
2011-04-28 15:08:12.051 XML[7053:20b] Processing Element: iid
2011-04-28 15:08:12.051 XML[7053:20b] Processing Value:
1
2011-04-28 15:08:12.052 XML[7053:20b] Processing Value:
2011-04-28 15:08:12.052 XML[7053:20b] Processing Element: subcatname
2011-04-28 15:08:12.052 XML[7053:20b] Processing Value:
card1
2011-04-28 15:08:12.053 XML[7053:20b] Processing Value:
2011-04-28 15:08:12.053 XML[7053:20b] Processing Element: url
2011-04-28 15:08:12.053 XML[7053:20b] Processing Value:
http://www.orkutpapa.com/scraps/happy-birthday-card-2-3.jpg
2011-04-28 15:08:12.054 XML[7053:20b] Processing Value:
here id my code:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict {
if([elementName isEqualToString:@"NewDataSet"]) {
//Initialize the array.
appDelegate.books = [[NSMutableArray alloc] init];
}
else if([elementName isEqualToString:@"Table"]) {
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:@"Table"]) {
[appDelegate.books addObject:aBook];
[aBook release];
aBook = nil;
}
else if([elementName isEqualToString:@"catname"])
{
[aBook setValue:currentElementValue forKey:@"catname"];
NSLog(@"elementName for catname....%@",currentElementValue);
NSLog(@"aBook for catname....%@",aBook);
}
else if([elementName isEqualToString:@"subcatname"])
{
[aBook setValue:currentElementValue forKey:@"subcatname"];
NSLog(@"elementName for catname....%@",currentElementValue);
NSLog(@"aBook for catname....%@",aBook);
}
else if ([elementName isEqualToString:@"url"]) {
[aBook setValue:currentElementValue forKey:@"url"];
// [aBook setValue:currentElementValue forKey:elementName];
NSLog(@"elementName for url....%@",currentElementValue);
NSLog(@"aBook for url....%@",aBook);
}
[currentElementValue release];
currentElementValue = nil;
}
Here is the xml code I would use:
Using an XML format above, ‘elements’ contains all of your xml records. ‘element’ represents one record with the following variables: id, catname, iid, subcatname, and url. You can change these to your preference, but pay attention to these variable names. This is how you will call the data when parsing.
You need to make a NSURLConnection somewhere. For example, this can be in response to a UITouch or placed in viewWillAppear. I am going to assume that you know how to make this connection, but it should contain the following methods:
This method should reset the data because a incomplete response was received.
This method is were you can append your data to an NSMutableData
This should handle errors, release the connection, and release the NSMutableData.
This method should call startParsingData and release the conneciton.
Now for the part you are probably interested in (sorry if you knew the NSURLConnection references above), how to parse the data:
In this method, you should initialize the data parser and assign the delegate. Make sure you assign the NSXMLParserDelegate protocol to your class.
Notice that attributeDict calls objectForKey. This key should match the variable name in the xml file that you want to call and use. Also notice that elementName (‘element’) matches the record name in the xml file.
Let me know if this helps or if you have any additional questions!
Cheers,
Evan