0

How i can make an xml file from NSString object?

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

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Yes it is for iphone but i think you link the question wrong, can you relink the question, thanks.
Sorry about that, it's fixed :)
Thanks. But is it possible not to make any physical copy of file and take string as xml document and process it? Thanks again.
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?
Yes that's what i am looking for.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.