views:

33

answers:

2

How i can make an xml file from NSString object?

In other words, how can I parse XML from an NSString object?

+1  A: 

If this is for the iPhone, you might want to take a look at this question which goes over doing it on the iPhone, using the user defaults method.

For reference, on Mac OS X one could use the writeToFile:atomically:encoding:error: NSString Method.

EDIT: To parse the XML within an NSString, you can use the NSXMLParser class. This class does not have a method to initialize it with an NSString, so you have to use its initWithData: method like this:

NSData *data = [myString dataUsingEncoding:NSUTF8StringEncoding];
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:data];

And that will initialize it with the NSString's data.

Of course, you have to learn how to parse the XML itself using this class. You can take a look at this question for some tips, or you can google around for some tutorials, such as Apple's very own walkthrough. Here is another walkthrough for using the class.

Jorge Israel Peña
Yes it is for iphone but i think you link the question wrong, can you relink the question, thanks.
itsaboutcode
Sorry about that, it's fixed :)
Jorge Israel Peña
Thanks. But is it possible not to make any physical copy of file and take string as xml document and process it? Thanks again.
itsaboutcode
Sorry, I don't understand your question. My understanding was that you wanted to store XML that was in an NSString, in a file. So in other words, save the NSString into an .xml file. What is it you want to do exactly? Take an NSString and parse the XML?
Jorge Israel Peña
Yes that's what i am looking for.
itsaboutcode
Oh okay, I added that information to the answer.
Jorge Israel Peña
A: 

If the xml you're parsing is in plist format, you can use the handy-dandy NSPropertyListSerialization class methods to parse it into a plist object for you.

Dave DeLong