我有一个这样的 XML 文件,我用NSXMLParser
它来解析它:
<? xml version ='1.0' encoding="UFT-8"?>
<!DOCTYPE plist PUBLIC "-//apple//DTD PLIST 1.0//EN" "http://www.apple/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<Data>
<item>
<date>2012-07-24</date>
<name>A</name>
<count>100</count>
<startDate>2012-07-24</startDate>
<stopDate>2012-07-24</stopDate>
</item>
<item>
<date>2012-07-24</date>
<name>A</name>
<count>100</count>
<startDate>2012-07-24</startDate>
<stopDate>2012-07-24</stopDate>
</item>
</Data>
</plist>
使用 IBAtion 来解析 xml 文件,例如
- (IBAction)Parser:(id)sender {
NSXMLParser *xmlParser = [[NSXMLParser alloc]initWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"file2" withExtension:@"xml"]];
[xmlParser setDelegate:self];
BOOL flag =[xmlParser parse];
if (flag) {
NSLog(@"OK");
} else {
NSLog(@"false");
}
[xmlParser release];
}
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI: (NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
NSLog(@"%@found a %@ element",self,elementName);
if ([elementName isEqual:@"Data"]) {
[data release];
data = [[Data alloc]init];
[data setParentParserDelegate:self];
[parser setDelegate:data];
}
}
有printf "OK"
和没有printf "NSLog(@"%@found a %@ element",self,elementName)"