views:

96

answers:

2

how to pass local path to NSXMLParser

like this

NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:storePath];

where storepath is a local path like this:

/var/mobile/Applications/56EEB906-2B73-493C-9BE7-2732440CDB69/Documents/xml1.php

and my code is like this:

//NSURL *urla = [[NSURL alloc] initWithString:storePath];
NSURL *urla = [[[NSURL alloc] init] fileURLWithPath:storePath];

//NSData *dataa = [NSData dataWithContentsOfURL:urla];

NSLog(@"urls ois %@",urla);

help me why i am getting null value

A: 

Try this one:

NSArray *paths = NSSearchPathForDirectoriesInDomains(
                     NSDocumentDirectory,
                     NSUserDomainMask, YES
                 );
NSString *documentsDirectory = [paths objectAtIndex:0];
NSURL *urla = [NSURL fileURLWithPath:[documentsDirectory
                         stringByAppendingString:@"/xml1.php"]];

More details at iPhone Application Programming Guide: Getting Paths to Application Directories.

Update: There's a common misunderstanding what stringByTrimmingCharactersInSet. It does not replace all whitespace in the string, it only removes leading and trailing whitespace.

If you want to get rid of the spaces, you need to replace them yourself by using something like stringByReplacingCharactersInRange:withString:.

However:

  • I am not sure what you would replace those strings with to get a valid path. As far as I know, on the simulator the path to the application's Documents folder actually has spaces in it (which accidentally is called out in the document I linked).
  • I am not sure why would the spaces bother you. That string is a valid path (as long as you actually have created the <app>/Documents/xml1.php file, of course); and according to NSXMLParser documentation, initWithContentsOfUrl accepts any fully qualified URL with scheme supported by NSURL, and file:// is supported scheme.

If there's a particular error the NSXMLParser is returning, add it to your question (or ask another one), so we can help you with the actual problem.

Franci Penov
thanks but how to remove those space??file://localhost/Users/applefan/Library/Application%20Support/iPhone%20Simulator/3.1.3/Applications/134C5D58-D930-4F0A-B2F7-D25A170D6F09/Documents/xml1.php
ram
); NSString *documentsDirectory = [paths objectAtIndex:0]; documentsDirectory = [documentsDirectory stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; NSLog(@"documentsDirectory is %@",documentsDirectory ); NSURL *urlc = [NSURL fileURLWithPath:[documentsDirectory stringByAppendingString:@"/xml1.php"]]; //urlc = [urlc stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; NSLog(@"urls is %@",urlc);urlc is still having space plz help
ram
like this ile://localhost/Users/applefan/Library/Application%20Support/iPhone%20Simulator/3.1.3/Applications/FD5A8AF3-0B07-4D56-8477-97F0DCEF15B5/Documents/xml1.php
ram
HiNSString *documentsDirectory = [paths objectAtIndex:0]; documentsDirectory = [documentsDirectory stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; //documentsDirectory = [documentsDirectory stringByAddingPercentEscapesUsingEncoding:(NSStringEncoding)NSUTF8StringEncoding]; NSURL *urlc = [NSURL fileURLWithPath:[documentsDirectory stringByAppendingString:@"/xml1.php" ]]; here my documentdirctly is working fine wihtout any space ,, but when i pass this to NSurl then i dont know why he is adding those %20 in between
ram
RFC 3986 (http://tools.ietf.org/html/rfc3986) specifies the allowed characters in an URL. In particular, space is not included in the set of allowed characters, therefore it needs to be percent-encoded, as per paragraph 2.1 in the same RFC. Hence the `%20`.
Franci Penov
And you still haven't said what exactly is the problem with the spaces or `%20` in the URL. Is `NSURL` or `NSXMLParser` complaining about the URL?
Franci Penov
wow thakns its working on device :)
ram
umh, how about accepting my answer then? :-)
Franci Penov
A: 

My guess would be because you're reading the PHP file directly instead of passing it through PHP so the output isn't XML.

Wevah