Problem with XML parser

Posted by zp26 on Stack Overflow See other posts from Stack Overflow or by zp26
Published on 2010-06-06T14:58:47Z Indexed on 2010/06/06 15:02 UTC
Read the original article Hit count: 353

Filed under:
|
|

Hi, I have a problem with parsing XML. I have created a program which write a file xml in the project directory. The file XML are correct. (i checked). When i try to read this XML the program crash and return 1 status. I have controlled my 2 path and they are equals. Can you help me please? Thanks so much.

#import "PositionIdentifierViewController.h"
#import "WriterXML.h"


@implementation PositionIdentifierViewController

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
    NSString *stringa = [NSString stringWithFormat:@"%@",string];
    textArea.text = [textArea.text      stringByAppendingString:@"\n"];
    textArea.text = [textArea.text      stringByAppendingString:stringa];


}


-(IBAction)startParsing
{
    NSURL *xmlURL = [NSURL fileURLWithPath:path];
    NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];
    [parser setDelegate:self];
    BOOL success = [parser parse];

    if(success == YES){
        //
    }
    [parser release];
}


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];


    NSArray *tempPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,  NSUserDomainMask, YES);
    NSString *documentsDirectoryPath = [tempPaths objectAtIndex:0];
    path = [documentsDirectoryPath  stringByAppendingPathComponent:@"filePosizioni.xml"];


    WriterXML *newWriter;
    newWriter = [[WriterXML alloc]init];
    [newWriter  saveXML:(NSString*)@"ciao":(float)10:(float)40:(float)70];
    [newWriter  saveXML:(NSString*)@"pippo":(float)20:(float)50:(float)80];
    [newWriter  saveXML:(NSString*)@"pluto":(float)30:(float)60:(float)90];
    NSLog(path);


}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end



#import "WriterXML.h"


@implementation WriterXML


-(void)saveXML:(NSString*)name:(float)x:(float)y:(float)z{

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,  NSUserDomainMask, YES);
    NSString *documentsDirectoryPath = [paths objectAtIndex:0];
    NSString *filePath = [documentsDirectoryPath  stringByAppendingPathComponent:@"filePosizioni.xml"];

    NSFileHandle *myHandle;
    NSFileManager *fileManager = [NSFileManager defaultManager];

    NSString *titoloXML = [NSString stringWithFormat:@"<?xml version=1.0 encoding=UTF-8 ?>"];
    NSString *inizioTag = [NSString stringWithFormat:@"\n\n\n<position>"];
    NSString *tagName = [NSString   stringWithFormat:@"\n <name>%@</name>", name];
    NSString *tagX = [NSString  stringWithFormat:@"\n  <x>%f</x>", x];
    NSString *tagY = [NSString  stringWithFormat:@"\n  <y>%f</y>", y];
    NSString *tagZ = [NSString  stringWithFormat:@"\n  <z>%f</z>", z];
    NSString *fineTag= [NSString    stringWithFormat:@"\n</position>"];


    NSData* dataTitoloXML = [titoloXML dataUsingEncoding: NSASCIIStringEncoding];
    NSData* dataInizioTag = [inizioTag dataUsingEncoding: NSASCIIStringEncoding];
    NSData* dataName = [tagName dataUsingEncoding: NSASCIIStringEncoding];
    NSData* dataX = [tagX dataUsingEncoding: NSASCIIStringEncoding];
    NSData* dataY = [tagY dataUsingEncoding: NSASCIIStringEncoding];
    NSData* dataZ = [tagZ dataUsingEncoding: NSASCIIStringEncoding];
    NSData* dataFineTag = [fineTag dataUsingEncoding: NSASCIIStringEncoding];


    if(![fileManager fileExistsAtPath:filePath])
        [fileManager createFileAtPath:filePath  contents:dataTitoloXML attributes:nil];

    myHandle  = [NSFileHandle fileHandleForUpdatingAtPath:filePath];

    [myHandle seekToEndOfFile];
    [myHandle   writeData:dataInizioTag];
    NSLog(@"writeok");

    [myHandle seekToEndOfFile];
    [myHandle   writeData:dataName];
    NSLog(@"writeok");

    [myHandle seekToEndOfFile];
    [myHandle   writeData:dataX];
    NSLog(@"writeok");

    [myHandle seekToEndOfFile];
    [myHandle   writeData:dataY];
    NSLog(@"writeok");

    [myHandle seekToEndOfFile];
    [myHandle   writeData:dataZ];
    NSLog(@"writeok");

    [myHandle seekToEndOfFile];
    [myHandle   writeData:dataFineTag];
    NSLog(@"writeok");

    [myHandle seekToEndOfFile];

    NSLog(@"zp26 %@",filePath);
}   

@end

© Stack Overflow or respective owner

Related posts about iphone

Related posts about objective-c