我的 XML 文件是:
<?xml version="1.0" encoding="UTF-8"?>
<plist version="1.0">
<count count="3" />
<spac>
<opt>aa</opt>
<opt>bb</opt>
</spac>
</plist>
我为 NSXML parssr 使用了以下代码行:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict {
if([elementName isEqualToString:@"spaces"]) {
//Initialize the array.
appDelegate.api = [[NSMutableArray alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
[appDelegate.api addObject:string];
NSLog(@"the count is :%d", [appDelegate.api count]);
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if([elementName isEqualToString:@"spaces"])
return;
}
但我在 gdb 得到以下输出,但我无法找出原因:
2012-06-05 02:20:57.940 XML[490:f803] the count is :0
2012-06-05 02:20:57.942 XML[490:f803] the count is :0
2012-06-05 02:20:57.943 XML[490:f803] the count is :1
2012-06-05 02:20:57.944 XML[490:f803] the count is :2
2012-06-05 02:20:57.945 XML[490:f803] the count is :3
2012-06-05 02:20:57.946 XML[490:f803] the count is :4
2012-06-05 02:20:57.946 XML[490:f803] the count is :5
2012-06-05 02:20:57.948 XML[490:f803] the count is :6
2012-06-05 02:20:57.948 XML[490:f803] the count is :7
2012-06-05 02:20:57.949 XML[490:f803] the count is :8
有人可以帮我吗?我是目标 C 的新手。谢谢。