[英]How do I parse an NSString containing XML in Objective-C?
In my iPhone application, I have the following NSString: 在我的iPhone应用程序中,我有以下NSString:
NSString *myxml=@"<students>
<student><name>Raju</name><age>25</age><address>abcd</address>
</student></students>";
How would I parse the XML content of this string? 我该如何解析这个字符串的XML内容?
Download: https://github.com/bcaccinolo/XML-to-NSDictionary 下载: https : //github.com/bcaccinolo/XML-to-NSDictionary
Then you simply do : 然后你只需:
NSDictionary *dic = [XMLReader dictionaryForXMLString:myxml error:nil];
Result is a NSDictionary *dic with dictionaries, arrays and strings inside, depending of the XML: 结果是一个NSDictionary * dic,其中包含字典,数组和字符串,具体取决于XML:
{
students = {
student = {
address = abcd;
age = 25;
name = Raju;
};
};
}
You should use the NSXMLParser class 您应该使用NSXMLParser类
Here's a link to the documentation for that class: http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSXMLParser_Class/Reference/Reference.html 以下是该类文档的链接: http : //developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSXMLParser_Class/Reference/Reference.html
Your code should look something like this: 您的代码应如下所示:
@implementation MyClass
- (void)startParsing {
NSData *xmlData = (Get XML as NSData)
NSXMLParser *parser = [[[NSXMLParser alloc] initWithData:xmlData] autorelease];
[parser setDelegate:self];
[parser parse];
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict {
NSLog(@"Started %@", elementName);
}
Another answer is: Don't use XML. 另一个答案是:不要使用XML。 Use a plist instead, which is written using XML but more easily parsable in Objective-C into distinct data types (NSArray for example has a method to convert a file or NSData plist into an NSArray).
使用plist代替,它使用XML编写,但在Objective-C中更容易解析为不同的数据类型(例如,NSArray有一个方法将文件或NSData plist转换为NSArray)。
Like @Jon Hess mentioned, just create a wrapping class for the "optional" methods of the NSXMLParserDelegate. 就像@Jon Hess提到的那样,只需为NSXMLParserDelegate的“可选”方法创建一个包装类。 These methods help you separate the tasks that you might find useful when you parse your xml.
这些方法可帮助您分离在解析xml时可能会发现有用的任务。
One really good online journal file I found is Elegant XML parsing with Objective-C . 我发现一个非常好的在线日志文件是使用Objective-C进行优雅的XML解析 。 Phil Nash really took his time to show the basics of the parsing options at your reach.
Phil Nash真的花了很多时间来展示解决方案的基础知识。 It can take a new programmer and guide him/her through the whole setup.
它可以需要一个新的程序员并引导他/她完成整个设置。
Loading the xml can be a modification of @Jon Hess method. 加载xml可以修改@Jon Hess方法。
You can setup the: 你可以设置:
-(void)parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
attributes:(NSDictionary *)attributeDict{
}
to handle events on certain elements. 处理某些元素的事件。
Also implement the: 同时实现:
-(void)parser:(NSXMLParser *)parser
foundCharacters:(NSString *)string {
}
to place the strings found into a collection of objects. 将找到的字符串放入对象集合中。
I think the best equivalent to XMLDocument is AbacigilXQ Library . 我认为与XMLDocument最相同的是AbacigilXQ Library 。 You should look at it.
你应该看看它。 I'm using it.
我正在使用它。
http://code.google.com/p/abacigilxq-library/ http://code.google.com/p/abacigilxq-library/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.