XML FILE CODE:
<Books>
<Book id="1" isbn="123456">
<name>Let Us C</name>
<authors>
<author>Yashwant Kanetkar</author>
<author>ABC</author>
</authors>
<price>250.00</price>
</Book>
<Book id="2" isbn="345678">
<name>Programing With C++</name>
<authors>
<author>Balaguruswamy</author>
<author>XYZ</author>
</authors>
<price>400.00</price>
</Book>
<Book id="3" isbn="789012">
<name>Professional iPhone and iPad Application Development</name>
<authors>
<author>Gene Backlin</author>
<author>QWE</author>
</authors>
<price>550.00</price>
</Book>
<Book id="4" isbn="901234">
<name>Beginning iOS 4 Application Development</name>
<authors>
<author>Wei-Meng Lee</author>
<author>IOP</author>
</authors>
<price>500.00</price>
</Book>
</Books>
Can anyone pls help me with this?
I'm using this code:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict {
if([elementName isEqualToString:@"Books"])
{
appDelegate.books = [[NSMutableArray alloc] init];
}
else if([elementName isEqualToString:@"Book"])
{
aBook = [[Book alloc] init];
aBook.bookID = [[attributeDict objectForKey:@"id"] integerValue];
aBook.isbn = [[attributeDict objectForKey:@"isbn"] integerValue];
NSLog(@"Reading isbn value : %@", aBook.isbn);
NSLog(@"Reading id value :%i", aBook.bookID);
}
NSLog(@"Processing Element: %@", elementName);
}
First take a bool variable. and then write the following code. perhaps it will help you I have done the related work , a few days ago.
First .h file
@interface EventsViewController : UIViewController <NSXMLParserDelegate>
{
NSMutableArray *_idArray;
NSMutableArray *_isbnArray;
BOOL isBook;
}
Then in .m file you should write this.
//In ViewDidLoad
- (void)viewDidLoad
{
[super viewDidLoad];
_idArray = [[NSMutableArray alloc] init];
_isbnArray = [[NSMutableArray alloc] init];
NSString *xmlString = [[NSBundle mainBundle] pathForResource:@"today_in_history_short" ofType:@"xml"];
NSURL * fileURL = [NSURL fileURLWithPath:xmlString];
NSXMLParser * parser = [[NSXMLParser alloc] initWithContentsOfURL:fileURL];
[parser setDelegate:self];
[parser parse];
}
// Now In xml Parser Delegate Methods
pragma mark - NSXMLParser Delegate Method
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
if ([elementName isEqualToString:@"Books"]) {
isBook = YES;
}
else if ([elementName isEqualToString:@"Book"] && isBook){
NSString *idString = [attributeDict objectForKey:@"id"];
NSString *isbnString = [attributeDict objectForKey:@"isbn"];
[_idArray addObject:idString];
[_isbnArray addObject:isbnString];
NSLog(@"Book id is: %@ and Book isbn is: %@",idString,isbnString);
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if ([elementName isEqualToString:@"Books"]) {
isBook=NO;
NSLog(@"Id Array count is :%d",[_idArray count]);
}
}
Note: I have check it , It's working , you can use it in you code , if still it's won't working then there will be something else wrong in your project, you can verify it by making separate project.