如何在 Objective-C 中解析包含 XML 的 NSString?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/924389/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:37:17  来源:igfitidea点击:

How do I parse an NSString containing XML in Objective-C?

iphoneobjective-cxml

提问by Raju

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 内容?

回答by C?ur

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;
        };
    };
}

回答by Jon Hess

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);
}

回答by Kendall Helmstetter Gelner

Another answer is: Don't use 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).

另一个答案是:不要使用 XML。改用 plist,它是用 XML 编写的,但在 Objective-C 中更容易解析为不同的数据类型(例如,NSArray 有一种将文件或 NSData plist 转换为 NSArray 的方法)。

回答by A Salcedo

Like @Jon Hess mentioned, just create a wrapping class for the "optional" methods of the NSXMLParserDelegate. These methods help you separate the tasks that you might find useful when you parse your xml.

就像@Jon Hess 提到的那样,只需为 NSXMLParserDelegate 的“可选”方法创建一个包装类。这些方法可帮助您分离在解析 xml 时可能有用的任务。

One really good online journal file I found is Elegant XML parsing with Objective-C. Phil Nash really took his time to show the basics of the parsing options at your reach. It can take a new programmer and guide him/her through the whole setup.

我发现的一个非常好的在线日志文件是Elegant XML parsing with Objective-C。Phil Nash 真的花时间展示了您可以访问的解析选项的基础知识。它可能需要一个新程序员并指导他/她完成整个设置。

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.

将找到的字符串放入对象集合中。

回答by kaya kahveci

I think the best equivalent to XMLDocument is AbacigilXQ Library. You should look at it. I'm using it.

我认为与 XMLDocument 最好的等价物是AbacigilXQ Library。你应该看看它。我正在使用它。

http://code.google.com/p/abacigilxq-library/

http://code.google.com/p/abacigilxq-library/