我尝试使用NSXMLParser类解析XML格式的NSString,但我无法分析它。 它只显示第一个标签(元素)的元素 当我解析XML文件中的内容时,我得到了所有元素。
#import "Parser.h"
#import "DeviceConfig.h"
@implementation Parser
@synthesize curElement;
@synthesize value;
@synthesize defaultValue;
-(id) initWithXMLData: (NSString*) data {
if(self == [super init]) {
xmlData = data;
NSData* xmlNSData = [xmlData dataUsingEncoding:NSUTF8StringEncoding];
cameraOptions = [[NSMutableDictionary alloc] init];
parser = [[NSXMLParser alloc] initWithData:xmlNSData];
[parser setDelegate: self];
[parser parse];
}
return self;
}
- (NSMutableDictionary*) getCameraOptions {
return cameraOptions;
}
- (void) parser: (NSXMLParser*) parser didStartElement:(nonnull NSString *)elementName namespaceURI:(nullable NSString *)namespaceURI qualifiedName:(nullable NSString *)qName attributes:(nonnull NSDictionary<NSString *,NSString *> *)attributeDict {
NSLog(@"started element : %@", elementName);
}
- (void) parser: (NSXMLParser*) parser didEndElement:(nonnull NSString *)elementName namespaceURI:(nullable NSString *)namespaceURI qualifiedName:(nullable NSString *)qName {
NSLog(@"ended element : %@", elementName);
}
- (void) parser: (NSXMLParser*) parser foundCharacters:(nonnull NSString *)string {
}
@end
主
int main(int argc, const char * argv[]) {
@autoreleasepool {
Parser* p = [[Parser alloc] initWithXMLData:@"<?xml version=\"1.0\" encoding=\"utf-8\"?><flash></flash><torch></torch>"];
}
return 0;
}