我試圖解析xml文件並且沒有錯誤,但在嘗試讀取它時解析器:didStartElement事件未觸發。我做錯了什麼?感謝幫助。當你分配/初始化NSXMLParser解析器:didStartElement在解析xml時未觸發
[xmlParser setDelegate:self];
:
- (void)viewDidLoad
{
[super viewDidLoad];
// xml connect
NSURL *url = [[NSURL alloc] initWithString:@"http://www.test.com/list.xml"];
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
//Start parsing the XML file.
BOOL success = [xmlParser parse];
if(success)
NSLog(@"No Errors");
else
NSLog(@"Error!");
}
// reading xml...
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict {
if([elementName isEqualToString:@"main"]) {
//Initialize the array.
apps = [[NSMutableArray alloc] init];
}
else if([elementName isEqualToString:@"prog"]) {
//Extract the attribute here.
idUsuari = [attributeDict objectForKey:@"Id"];
NSLog(@"ID: %@", idUser);
}
}
工作後,非常感謝! – Jaume