How to read a local xml file - iphone

I want to create a NSURL object out of a NSString, where I use -fileURLWithPath:(NSString )
I put my xml file as my source file, and name it event.xml.
But
NSString URL = #"event.xml";
NSURL a = [NSURL fileURLWithPath:URL];
but then my xmlparser return a connection failed error.
so what's the correct way to specify the path of a xml in the source file?
sorry about the formatting but I'm really in a rush

That depends on the directory your XML file is in. If we are talking about a file in your app bundle, it is:
NSString *path = [[NSBundle mainBundle] pathForResource:#"event" ofType:#"xml"];

Related

How to convert sound file to binary file and send in http post?

I recorded sound in file called "recordedTmpFile".now i want to send the file to server .
i tried this code
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"recordedTmpFile" ofType:#"caf"];
NSData *postData = [filePath dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
//nsdata to string
NSString *content = [[NSString alloc] initWithBytes:[postData bytes]
length:[postData length] encoding: NSUTF8StringEncoding];
am getting null value in "content"
please help me to fix this problem
Did your app record the sound and write it into the file? Remember that the app bundle is read-only, so if you tried to write the file to a location in the main bundle, as your code seems to indicate, then the write operation probably failed. If that's the case, then of course there's no file to read or send.
Try changing the path to which you record data to someplace in the documents directory, which is writeable.

can i update my local text file on iphone

i have a text file so can i update this text file from web ???
lets say i have
1. NSString *filePath = [[NSBundle mainBundle] pathForResource:#"MyFile" ofType:#"txt"];
2. NSData *myData = [NSData dataWithContentsOfFile:filePath];
3. if (myData) {
4. // do something useful
5. }
right now Myfile.text is having 10 data so how to insert 10 more to this data??
plz help
Files stored inside your app bundle will not be writable.
You will have to make a copy of the file and store it in the Documents or Library folder before you will be able to edit it.

iPhone Referring to Resources in Separate Directories

This question tells how one can create directories in your resources path. Once created, how does one reference these directories?
I have created an html directory with a structure for my internal pages, now I want to load the index.html file from the html directory. Thus, I'll need the file path to it.
I can just use:
NSString *filename = [[NSBundle mainBundle] pathForResource:#"index"
ofType:#"html"];
But what happens if there are two index.html files contained in the directory structure? Does it just find the first one? Can the referencing be more specific?
Little test project here that is not working if you want to take a look
If you have a path that is copied into your resource bundle, you also should reference the path when looking for the resource like:
NSString *filename = [[NSBundle mainBundle] pathForResource:#"html/index" ofType:#"html"];
EDIT:
You cannot apparently just include the directory in the resource name (I believe you can for other similar calls like "imageNamed:" on UIImage). The working call is:
NSString *helpFile = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html" inDirectory:#"html"];

read single line from text file in objective-C

i'm new to iPhone programming and coding in XCode SDK.I want to access and read the configuration file which i have placed in Resource folder in XCode,my configuration file looks like this
#key=value$
#vinu=flower$
#cathy=fruit$
I want to compare the key and access the value from configuration file.
Since i'm working with iPhone OS, i cant use NSWorkspace.Hence i'm using NSFileHandle.
This is how my code looks like,
NSString *path = [[NSBundle mainBundle] pathForResource:#"configuration" ofType:#"txt"];
NSFileHandle *readHandle = [NSFileHandle fileHandleForReadingAtPath:path];
NSString *txtString = [[NSString alloc] initWithData:
[readHandle readDataToEndOfFile] encoding:NSUTF8StringEncoding];
please let me know is the procedure correct, and how to proceed. ??
Thank You.
Do yourself a favor and save it as a plist, and not a straight text file.
However, if you need to read it in like that, the simplest way is to read it into a string and then go from there, ie:
NSString * fileContents = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
If the file is super large and the only reasonable way to read it is line-by-line, then you can quickly see that this is something that has come up before (Particularly: Objective-C: Reading a file line by line).

Loading data files in iPhone project

How does one read a data file in an iPhone project? For example, lets say I have a static file called "level.dat" that is structured as follows:
obstacles: 10
time: 100
obstacle1: 10,20
...
I would like to read the contents of the file into a NSString then do the parsing. How do I read the contents of a file into a string? Also, where in the project should the "level.dat" file reside? Should it be under "Resources" or just in the main directory?
Thanks in advance!
See this answer: How to fopen() on the iPhone? which shows how to get access to resources in your bundle. Once you have the path, just use [NSString stringWithContentsOfFile:encoding:error:].
NSString *path = [[NSBundle mainBundle] pathForResource: #"level" ofType: #"dat"]
NSError *error = nil;
NSString *data = [NSString stringWithContentsOfFile: path
encoding: NSUTF8StringEncoding
error: &error];
While this isn't what you asked for, consider turning your files into plists. You will have to reformat them into XML, but then you can load them straight into a NSDictionary with:
dict = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"levels" ofType:#"plist"]];
Have you considered putting the data in an SQLite database instead of a flat file? I find that the API is very easy to use on the iPhone.
It is how I do all of my data storage on the phone now.
If you need help parsing the data string, there's a helpful article on Cocoa For Scientist