开发者

Represent XML content as NSData - iPhone

开发者 https://www.devze.com 2023-02-05 13:50 出处:网络
I have an external XML file开发者_运维百科 which I want to be represented in an NSData object I am doing this;

I have an external XML file开发者_运维百科 which I want to be represented in an NSData object

I am doing this;

 NSError *error;
NSString* contents = [NSString stringWithContentsOfUrl:[NSURL URLWithString:@"http://www.apple.com/"]
                                              encoding:NSUTF8StringEncoding
                                                 error:&error];
NSData* xmlData = [contents dataUsingEncoding:NSUTF8StringEncoding];

But for some reasons, I am getting an error (does not respond)

Please help me.


Don't have time to test this out but I think you may want to try looking into NSData's dataWithContentsOfURL: or dataWithContentsOfURL:options:error: and get it directly as data.

Also, unless you just threw http://www.apple.com/ in as a placeholder, I don't believe the source of that page is valid XML. The following feed is valid xml: https://stackoverflow.com/feeds You could try that. with what you have now first and see if it works.

Hope this helps.

Updated:

Without my knowing your project, you may get some benefit from using TouchXML - https://github.com/mrevilme/TouchXML which handles XML very well including what you are trying to do:

CXMLDocument *xmlDoc = [[CXMLDocument alloc] initWithContentsOfURL:(NSURL *)inURL encoding:(NSStringEncoding)encoding options:(NSUInteger)inOptions error:(NSError **)outError];
0

精彩评论

暂无评论...
验证码 换一张
取 消