NSXMLParser解析器:解析xml时未触发didStartElement
我正在尝试解析 xml 文件并且没有收到错误,但是在尝试读取它时,解析器:didStartElement 事件未触发。我做错了什么?感谢您的帮助。
- (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);
}
}
I am trying to parse xml file and get no errors but when trying to read it, parser:didStartElement event is not triggered. What I am doing wrong? Thanks for help.
- (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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您尚未将视图控制器设置为 XML 解析器的委托:
在分配/初始化之后。如果没有这个,解析器就不知道将委托消息发送给谁。
You haven't set your view controller as the XML Parser's delegate:
After you alloc / init. Without this the parser does not know who to send the delegate messages to.
然后在解析之前声明您当前的类符合
NSXMLParserDelegate
,以便解析器知道可以调用您实现的委托方法。
例如
declare that your current class conforms to
NSXMLParserDelegate
, thenbefore you parse, so the parser knows can call the delegate methods implemented by you.
eg
再次运行后请将此添加到 Info.plist
Please add this to Info.plist after run again