简体   繁体   English

使用Objective C的xml字符串文件

[英]xml string to file using Objective C

How i can make an xml file from NSString object? 我如何从NSString对象制作xml文件?

In other words, how can I parse XML from an NSString object? 换句话说,如何从NSString对象解析XML?

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. 如果这是为iPhone,你可能想看看这个问题,它越过做它在iPhone上使用的用户默认方法。

For reference, on Mac OS X one could use the writeToFile:atomically:encoding:error: NSString Method. 作为参考,在Mac OS X上,可以使用writeToFile:atomically:encoding:error: NSString方法。

EDIT : To parse the XML within an NSString, you can use the NSXMLParser class. 编辑 :要解析NSString中的XML,可以使用NSXMLParser类。 This class does not have a method to initialize it with an NSString , so you have to use its initWithData: method like this: 此类没有使用NSString初始化它的方法,因此您必须使用其initWithData:方法,如下所示:

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

And that will initialize it with the NSString 's data. 然后将使用NSString的数据对其进行初始化。

Of course, you have to learn how to parse the XML itself using this class. 当然,您必须学习如何使用此类解析XML本身。 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 . 您可以查看此问题的一些技巧,也可以在Google周围搜索一些教程,例如Apple自己的演练 Here is another walkthrough for using the class. 这是使用该类的另一个演练。

如果您要解析的xml为plist格式,则可以使用方便的NSPropertyListSerialization类方法为您解析为一个plist对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM