NSXMLParser init with XML in NSString format
Asked Answered
M

3

10

I just ran into this one and couldn't seem to get any clear answer from the documentation.

Im retrieving some XML through a HTTPS connection. I do all sorts of authentication etc. so I have a set of classes that deals with this in a nice threaded way. The result is an NSString that goes something like:

<response>
//some XML formatted test
</response>

This means that there is no encoding="UTF-8" indent="yes" method="xml" or other header blocks to indicate that this is actual XML and not just an NSString.

I guess I will use [NSXMLParser initWithData:NSData] to construct the parser, but how will I format or cast my NSString of xml formatted text into a proper NSData object that NSXMLParser will understand and parse?

Hope it makes sense, thank you for any help given :)

Monitorial answered 3/12, 2009 at 16:13 Comment(0)
T
31

You can convert a string to a NSData object using the dataUsingEncoding method:

NSData *data = [myString dataUsingEncoding:NSUTF8StringEncoding];

You can then feed this to NSXMLParser.

Tobe answered 3/12, 2009 at 16:17 Comment(1)
Thanks Philippe, I will try this out straight away.Monitorial
W
2

The headers are optional, but you can insert the appropriate headers yourself if needed:

NSString *header = @"<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
NSString *xml = [NSString stringWithFormat:@"%@\n%@", header, response);
NSData *data = [xml dataUsingEncoding:NSUTF8StringEncoding];
NSXMLParser *praser = [NSXMLParser initWithData:data];
Watts answered 3/12, 2009 at 16:19 Comment(1)
Thanks for the suggestion/code notnoop! For my use I'd rather just go on with parsing, but I guess if I was passing this on to an object that would only accept "real" xml this would be the way to go.Monitorial
A
1

By the time NSXMLParser has the data it's pretty much going to be expecting it to be XML ;-)

I'm pretty sure the processing instruction header is optional in this context. The way you get the NSString into the NSData is going to dictate the encoding (using dataUsingEncoding:).

(edit: I was looking for the encoding enum, but Philippe Leybaert beat me to it, but to repeat it here anyway, something like: [nsString dataUsingEncoding:NSUTF8StringEncoding])

I've passed NSStrings of XML in this way before with no issues.

Not specific to this question as such, but on the subject of XML parsing in an iPhone context in general you may find this blog entry of mine interesting, too.

Ambroseambrosi answered 3/12, 2009 at 16:18 Comment(3)
It is optional, yes, and should generally be omitted if it's just 1.0/UTF-8. Though technically the XML Declaration isn't a Processing Instruction, even though it looks like one.Doradorado
yeah, I thought someone would pick up on that - couldn't think of a better way of referring to it - but accuracy is important, you are right.Ambroseambrosi
Thank You Phil:) I did find it interesting! especially that you wrap some of the SAX stuff an abstraction level upwards, that will surely come in handy.Monitorial

© 2022 - 2024 — McMap. All rights reserved.