how to pass local path to NSXMLParser - cocoa

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

Hey THis all some what not fair.
i suggest this, very simple
NSString *path=#"/Users/farshore/Desktop/prasanna/XMLParsing/Books-6.xml"; //this is ur path of xml file
NSURL *url = [[NSURL alloc] initFileURLWithPath:path];

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.

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

Related

migratePersistentStore: causes duplicate data when destination URL exists

Just encountered a scenario that I thought I'd share. When using NSPersistentStoreCoordinator migratePersistentStore: with a destination URL that already exists, the resulting data is merged. My scenario is that I'm creating dated backups whenever my app closes and it's possible that multiple launches in the same day will backup so the same file.
I solved the issue by using NSFileManager fileExistsAtPath: and removeItemAtPath: to remove the existing file before calling migratePersistentStore:. This seems to have solved the duplication issue.
I couldn't find documentation that this is a feature but maybe it is.
UPDATED
I've added some example code for Jim. The flag for disabling journal_mode was very important for my use case. See here for more info
if ([[NSFileManager defaultManager] fileExistsAtPath: backupPath])
[[NSFileManager defaultManager] removeItemAtPath: backupPath
error: nil];
return [BRManagedObject.persistentStoreCoordinator
migratePersistentStore: store
toURL: [NSURL fileURLWithPath: backupPath]
options: #{
NSSQLiteManualVacuumOption : #(YES)
#ifndef SQLITE_USES_WRITE_AHEAD_LOG
, NSSQLitePragmasOption : #{ #"journal_mode" : #"DELETE" }
#endif
}
withType: persistentStoreType
error: nil];

Fetching data from localhost

I'm using XAMPP and I'm trying to fetch some data out of a php file.
If I put the file on my webserver, it works, but locally it doesnt. Seems like Xcode doenst "find" my localhost.. any ideas?
NSString *stringURL = [NSString stringWithFormat:#"http://localhost/root/juraQuiz/test.php"];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:stringURL]];
NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(#"%#", result);
Unfortunately I can't comment yet, so I've had to answer.
I would try seeing if the url is actually resolvable by using NURLs method and check the error.
-(BOOL)checkResourceIsReachableAndReturnError:(NSError **)error
Its possible your local server isn't using normal http ports and is using something random which you'll have to include in the url i.e. "localhost:3000/page.php"
Also, for remote fetching of data maybe NSURLConnection or something similar is more appropriate?
Hope that helps in some way.

Is Core Data Lightweight Migration, on Lion, not saving a backup file?

Previously, when you did a lightweight migration, core data created a backup file in the same folder like ~yourFile. But using Lion, I can't find this backup file. Is the file hided somewhere? I did look the documentation, but without success.
Any help would be appreciated.
Many thanks
Mario
It seems the ˜file is no more automatically created. So the solution I found is to create the file programatically.
So, I'm posting my solution
// Determine if a migration is needed
NSDictionary *sourceMetadata = [NSPersistentStoreCoordinator metadataForPersistentStoreOfType:NSSQLiteStoreType URL:url error:&error];
NSManagedObjectModel *destinationModel = [persistentStoreCoordinator managedObjectModel];
BOOL pscCompatibile = [destinationModel isConfiguration:nil compatibleWithStoreMetadata:sourceMetadata];
// if migration is needed, copy the file appending OLD to the original name
if (pscCompatibile == 0) {
NSLog(#"doing migration");
NSURL *urlOld = [NSURL fileURLWithPath: [applicationSupportDirectory stringByAppendingPathComponent: #"AudioStManDataOLD"]];
[[NSFileManager defaultManager] copyItemAtURL:url toURL:urlOld error:&error];
}
tx
Mario

How do I distinguish between NSURLConnections in delegate methods?

I'm writing a RSS feeder and using the NSXMLParser to parse an XML page for me.
Everything works well.
This is the code that handles the asynchronously connection:
static NSString *feedURLString = #"http://www.example.com/data.xml";
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:feedURLString]];
feed = [[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease];
Now i'm trying to add another website to be parsed using the same code above, but i need to do different action, on a different URL
I'm implementing the delegate function:
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
The problem is i can't figure out which website was called, i only got the data.
How can i figure out which URL the connection is resolved to?
For example in the :
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
I can check the NSString in NSURL URL that comes from the NSURLResponse but in the above function I can't.
If you implement both delegate methods, shouldn't you receive the latter notification first? If so, you can associate the URL string with the NSURLConnection (which will assumedly be different for each site) in an instance variable and use it when you get the data afterward. I would generally suggest a dictionary, although you can't use the connection as a key in an NS(Mutable)Dictionary since it doesn't conform to NSCopying. You could use the URL string as the key, but that complicates lookup. Perhaps a pair of arrays?
More to the point though, why write an RSS reader from scratch? On 10.5+ you can use the PubSub.framework to do the work for you. This framework handles all sorts of weird formats and invalid XML, and can really save you a lot of time. Maybe it's a good fit for what you're trying to do?

How do I download contents into an NSData using a Secure URL?

I'm starting with the following snip to download an image
NSError *error = nil;
NSString *url = #"https://...";
[NSData dataWithContentsOfURL:[NSURL urlWithString:url] options:nil error:&error];
When this code runs, the error instance contains an error without a whole lot of information in the userInfo. It's just the secure url that was attempted.
Given the results, I'm pretty sure these methods don't handle secure URLs.
My question:
Is there an easy way (like these
methods, or some option) to set the
dataWithContentsOfURL: method to download over a secure url?
Try running your URL through this first

Resources