Parsing XML data to object classes



I wrote this code which takes data from an xml file and should istanziarmi an Event object for each user that is.


I tried to create me a NSMutableArray entering the data and at the end of the read cycle of each user voleov put them in a oggetot Event.


But the problem is that the inside if the data is read but when it inserisoc data written into the object that is out if the data is not written:



#import "mkViewController.h"
#import "evento.h"

@interface mkViewController ()
@property(nonatomic,strong)NSXMLParser *parser;
@property(nonatomic,strong)NSString *element;
@property(nonatomic,strong)NSMutableArray *contenitore;
@property(nonatomic,strong)evento *UtenteCorrente;
@property(nonatomic)BOOL stoParsandoUnUtente;
@end

@implementation mkViewController

- (void)viewDidLoad
{
[super viewDidLoad];

self.stoParsandoUnUtente = NO;


NSURL *url = [NSURL URLWithString:@"http://ift.tt/1EsM4zQ"];
self.parser = [[NSXMLParser alloc] initWithContentsOfURL:url];

[self.parser setDelegate:self];
[self.parser setShouldResolveExternalEntities:NO];
[self.parser parse];

NSLog(@"%@", self.UtenteCorrente.nome);



}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {

self.element = elementName;

if ([elementName isEqualToString:@"utente"])
{
NSLog(@"Trovato Utente");
self.stoParsandoUnUtente = YES;

self.contenitore = [NSMutableArray array];





self.UtenteCorrente = [[evento alloc]init];


}

}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{

NSString *nome = [[NSString alloc]init];
NSString *cognome = [[NSString alloc]init];
NSString *codiceFiscale = [[NSString alloc]init];
NSString *dominio = [[NSString alloc]init];

if(self.stoParsandoUnUtente)
{


if ([self.element isEqualToString:@"nome"])
{

[self.contenitore addObject:nome];



}
else if ([self.element isEqualToString:@"cognome"])
{


[self.contenitore addObject:cognome];


}

else if ([self.element isEqualToString:@"codiceFiscale"])
{
[self.contenitore addObject:codiceFiscale];
}

else if ([self.element isEqualToString:@"dominio"])
{
[self.contenitore addObject:dominio];
}



}



}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {

if ([elementName isEqualToString:@"utente"])
{
NSLog(@"Fine Utente");
self.stoParsandoUnUtente = NO;
self.UtenteCorrente.nome = self.contenitore[1];
self.UtenteCorrente.cognome = self.contenitore[3];
self.UtenteCorrente.codiceFiscale = self.contenitore[1];
self.UtenteCorrente.dominio = self.contenitore[0];



NSLog(@"Trovato utente %@ %@", self.UtenteCorrente.nome, self.UtenteCorrente.cognome);

}

}


@end

No comments:

Post a Comment