Translate

Wednesday, May 23, 2012

NSXML Parser

book.xml

<?xml version="1.0" encoding="UTF-8"?>
<data>

<books>
    <book>
        <bookid>1</bookid>
        <title><![CDATA[Book 1]]></title>
        <authors><![CDATA[Author 1]]></authors>
        <imageURL><![CDATA[image1.png]]></imageURL>
        <webURL><![CDATA[http://www.google.com]]></webURL>
</book>
    <book>
        <bookid>2</bookid>
        <title><![CDATA[Book 2]]></title>
        <authors><![CDATA[Author 2]]></authors>
        <imageURL><![CDATA[image2.png]]></imageURL>
        <webURL><![CDATA[http://www.google.com]]></webURL>
    </book>
   
</data>


.h File


#import <UIKit/UIKit.h>

@interface NSXMLParserViewController : UIViewController<NSXMLParserDelegate>
{
  NSMutableString *tagValue;
  NSMutableArray *tagArray;
    NSString *tagName;
}

@end


.m File

#import "NSXMLParser.h"

@implementation NSXMLParser

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    tagArray=[[NSMutableArray alloc]init];
   
    tagName=[[NSString alloc]initWithString:@""];
   
   NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:[NSData dataWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"book.xml"]]];
   
    [xmlParser setDelegate:self];
    [xmlParser parse];
   
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
    attributes:(NSDictionary *)attributeDict {
   
   
    if([elementName isEqualToString:@"title"]||[elementName isEqualToString:@"imageURL"]||[elementName isEqualToString:@"webURL"]||[elementName isEqualToString:@"bookid"]||[elementName isEqualToString:@"authors"]){
   
   // if([elementName isEqualToString:@"title"]||[elementName isEqualToString:@"maincat"]||[elementName isEqualToString:@"subcat"]||[elementName isEqualToString:@"author"]){
       
        tagValue=[[NSMutableString alloc]init];
       

    }
   
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
    [tagValue appendString:string];
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
   
    NSString *string;
   
    if([elementName isEqualToString:@"title"]){
        string=[tagValue stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
        NSLog(@"title %@",string);
        [tagArray addObject:string];
    }
   else if([elementName isEqualToString:@"imageURL"]){
        string=[tagValue stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
       NSLog(@"imageURL %@",string);
        [tagArray addObject:string];
    }
    else if([elementName isEqualToString:@"webURL"]){
        string=[tagValue stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
        NSLog(@"webURL %@",string);
        [tagArray addObject:string];
    }
    else if([elementName isEqualToString:@"authors"]){
        string=[tagValue stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
        NSLog(@"author %@",string);
        [tagArray addObject:string];
    }
    else if([elementName isEqualToString:@"bookid"]){
        string=[tagValue stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
        NSLog(@"bookid %@",string);
        [tagArray addObject:string];
    }
}

 

No comments:

Post a Comment