在我的iPhone应用程序中,我有以下NSString:
NSString *myxml=@""; Raju 25 abcd
我该如何解析这个字符串的XML内容?
下载:https: //github.com/bcaccinolo/XML-to-NSDictionary
然后你只需:
NSDictionary *dic = [XMLReader dictionaryForXMLString:myxml error:nil];
结果是一个NSDictionary*dic,其中包含字典,数组和字符串,具体取决于XML:
{ students = { student = { address = abcd; age = 25; name = Raju; }; }; }
您应该使用NSXMLParser类
以下是该类文档的链接:http: //developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSXMLParser_Class/Reference/Reference.html
您的代码应如下所示:
@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); }