0

I have a problem with a xml parsing. I have create a class for parsing. The xmlURL is correct (testing it from debug) but when i call the method parse the variable success become FALSE and a errorParsing is "NSXMLParserErrorDomain".

Can you help me? My code is below.

#import "xmlParser.h"
#import"Posizione.h"


@implementation xmlParser

@synthesize arrayPosizioniXML;

NSString *tempString;
Posizione *posizioneRilevata;

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

    if([tempString isEqualToString:@"name"])
        posizioneRilevata.nome = string;

    else if([tempString isEqualToString:@"x"])
        posizioneRilevata.valueX = [string  floatValue];

    else if([tempString isEqualToString:@"y"])
        posizioneRilevata.valueY = [string  floatValue];

    else if([tempString isEqualToString:@"z"]) 
        posizioneRilevata.valueZ = [string  floatValue];

    else if([tempString isEqualToString:@"/posizione"]) 
        [arrayPosizioniXML addObject:posizioneRilevata];
}


- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
    //[textArea setText:[[NSString alloc] initWithFormat:@"%@\nFine elemento: %@",textArea.text,elementName]];
}


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

    if ([elementName isEqualToString:@"name"]){
        tempString = @"name";
        NSLog(@"ok");
    }
    else if([elementName isEqualToString:@"x"]){
        tempString = @"x";
    }
    else if([elementName isEqualToString:@"y"]) {
        tempString = @"y";
    }
    else if([elementName isEqualToString:@"z"]) {
        tempString = @"z";
    }
    else if([elementName isEqualToString:@"/posizione"]) {
        tempString = @"/posizione";
    }
}


-(BOOL)avviaParsing{
    //Bisogna convertire il file in una NSURL altrimenti non funziona
    NSLog(@"zp26 %@",path);

    NSURL *xmlURL = [NSURL fileURLWithPath:path];
    // Creiamo il parser
    NSXMLParser *parser = [[ NSXMLParser alloc] initWithContentsOfURL:xmlURL];
    // Il delegato del parser e' la classe stessa (self)
    [parser setDelegate:self];
    //Effettuiamo il parser
    BOOL success = [parser parse];
    //controlliamo come è andata l'operazione
    if(success == YES){
        //parsing corretto
        return TRUE;
    } else {

        NSError *errorParsing = [[NSError alloc]init];
        errorParsing = [parser  parserError];
        //c'è stato qualche errore...
        return FALSE;
    }
    // Rilasciamo l'oggetto NSXMLParser
    [parser release];
}


-(id)init {
    self = [super init];
    if (self != nil) {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,  NSUserDomainMask, YES);
        NSString *documentsDirectoryPath = [paths objectAtIndex:0];
        path = [documentsDirectoryPath  stringByAppendingPathComponent:@"filePosizioni.xml"];

        posizioneRilevata = [[Posizione alloc]init];
        tempString = [[NSString alloc]init];
    }
    return self;
}

@end



@implementation AccelerometroViewController

-(BOOL)caricamentoXML{

    xmlParser *parser;

    parser = [[xmlParser alloc]init];

    if([parser  avviaParsing]){
        [arrayPosizioni addObjectsFromArray:parser.arrayPosizioniXML];
        return TRUE;
    }
    else
        return FALSE;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    if([self    caricamentoXML]){
        UIAlertView *alert = [[UIAlertView  alloc] initWithTitle:@"Caricamento Posizioni da Xml" message:@"Posizione caricata con successo" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
        [alert  show];
        [alert  release];
    }
    else{
        UIAlertView *alert = [[UIAlertView  alloc] initWithTitle:@"Caricamento Posizioni da Xml" message:@"Posizione non caricate" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
        [alert  show];
        [alert  release];
    }

}

@end

1 Answer 1

1

Try implementing this delegate methods:

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError;

It will call the delegate when the parser encounters an error.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.