NSXMLParser init,NSString格式的XML [英] NSXMLParser init with XML in NSString format
问题描述
我刚碰到这个,似乎无法从文档中得到任何明确的答案。
I just ran into this one and couldn't seem to get any clear answer from the documentation.
我通过HTTPS连接检索一些XML。我做各种身份验证等等所以我有一组以一个很好的线程方式处理这个问题的类。
结果是NSString类似于:
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>
这意味着没有编码=UTF-8indent =yesmethod = xml或其他标题块表示这是实际的XML而不仅仅是NSString。
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.
我想我将使用[NSXMLParser initWithData:NSData]来构造解析器,但是我如何格式化或将xml格式文本的NSString转换为NSXMLParser理解和解析的正确NSData对象?
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 :)
推荐答案
您可以使用dataUsingEncoding方法将字符串转换为NSData对象:
You can convert a string to a NSData object using the dataUsingEncoding method:
NSData *data = [myString dataUsingEncoding:NSUTF8StringEncoding];
然后您可以将其提供给NSXMLParser。
You can then feed this to NSXMLParser.
这篇关于NSXMLParser init,NSString格式的XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!