user2966615
user2966615

Reputation: 383

XML data parsing in xcode and storing it

i have a XML file which have 5 items(each have title and url) in it i want retrieve all of them in xcode using xml parsing and then want to store all the entries in arrays so i can use them in application. let me show you all my code.

XML File

<data>
<item>
<title>Eggs</title>
<link>
http://URL/ios/category.php?cat_id=14
</link>
</item>
<item>
<title>Bakery</title>
<link>
http://URL/ios/category.php?cat_id=15
</link>
</item>
<item>
<title>Bread</title>
<link>
http://URL/ios/category.php?cat_id=16
</link>
</item>
<item>
<title>Cakes, Pies Patisserie</title>
<link>
http://URL/ios/category.php?cat_id=17
</link>
</item>
<item>
<title>Specialty Breads</title>
<link>
http://URL/ios/category.php?cat_id=119
</link>
</item>
</data>

Here is my header file

#import "XMLStringFile.h"

@interface MyViewController : UIViewController<NSXMLParserDelegate>{
    NSMutableArray *rssOutputData;
    NSMutableString *nodecontent;
    NSXMLParser *xmlParserObject;
    XMLStringFile *xmlStringFileObject;
}

now in xcode here is my viewdidload code

rssOutputData = [[NSMutableArray alloc]init];
    NSData *xmlData=[[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:@"http://URL/mycategories.xml"]];
    xmlParserObject =[[NSXMLParser alloc]initWithData:xmlData];
    [xmlParserObject setDelegate:self];
    [xmlParserObject parse];

here is all the parsing methods

#pragma mark NSXMLParser delegate
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
{
    if([elementName isEqualToString:@"item"]){
        xmlStringFileObject =[[XMLStringFile alloc]init];
    } else {
        nodecontent = [[NSMutableString alloc] init];
    }
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    [nodecontent appendString:[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
    NSLog(@"node content = %@",nodecontent);
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    if([elementName isEqualToString:@"item"]){
        [rssOutputData addObject:xmlStringFileObject];
        xmlStringFileObject = nil;
    } else if([elementName isEqualToString:@"title"]){
        xmlStringFileObject.xmltitle= nodecontent;
        nodecontent = nil;
    }
    else if([elementName isEqualToString:@"link"]){
        xmlStringFileObject.xmllink= nodecontent;
        nodecontent = nil;
    }
}

Here is XMLStringFile.h and XMLStringFile.m

XMLStringFile.h

#import <Foundation/Foundation.h>

@interface XMLStringFile : NSObject {
    NSString *xmllink,*xmltitle;
}
@property(nonatomic,retain)NSString *xmllink,*xmltitle;
@end

And XMLStringFile.m file

#import "XMLStringFile.h"
@implementation XMLStringFile
@synthesize xmllink,xmltitle;
-(void)dealloc
{
}

Now i want to store my both entries in two separate arrays titles and links and then i want to retrieve them from those arrays in future. I need help please do let me know how can i do this. for now xmltitle and xmllink storing retrieved data but is only nslog last entire if i use it in another function.

Upvotes: 2

Views: 2506

Answers (1)

Kirit Modi
Kirit Modi

Reputation: 23407

Download XMLReader

In ViewDidLoad method.

- (void)viewDidLoad {

    NSData *xmlData=[[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:@"http://cms.proxiclients.com/choithrams/mycategories.xml"]];

    NSString *XMLString = [[NSString alloc]initWithData:xmlData encoding:NSUTF8StringEncoding];

    XMLString = [XMLString stringByReplacingOccurrencesOfString:@"\n" withString:@""];

    NSDictionary *dict = [XMLReader dictionaryForXMLString:XMLString error:nil];

    NSLog(@"== %@",dict);

    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

you can get dictionary value as below.

NSString *linkText = [[[[dict valueForKey:@"data"] valueForKey:@"item"] valueForKey:@"link"] valueForKey:@"text"];
linkText = [linkText stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

Your Output is Dictionary:

enter image description here

Upvotes: 3

Related Questions