我想解析這個XML使用NSXMLParser。我有「咖啡」的多個實例的XML文件中,就像這樣:NSXMLParser停止,當它發現第一個實例
… More XML
<Coffee>
<Type Name="Type">Minimum Drinking Amount</Type>
</Coffee>
<Coffee>
<Type Name="Type">Maximum Drinking Amount</Type>
</Coffee>
… More XML
現在,一旦發現的NSXMLParser「咖啡」的第一個實例,完成並移動到XML的其餘部分。哪個...好吧,不是我想要做的。我需要它來讀取「Coffee」的每個實例這就是我在Objective-C中處理它的方式。
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
NSLog(@"Fired Stick1");
_xmlString = elementName;
if ([_xmlString isEqualToString:@"Coffee"]) {
NSLog(@"Stick1 Coffee:");
}
if ([_xmlString isEqualToString:@"Tea"]) {
NSLog(@"Stick1 Tea:");
}
然後foundCharacters代表:
-(void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if ([_xmlString isEqualToString:@"Coffee"] || [_xmlString isEqualToString:@"Tea"]) {
[email protected]"%@ Called", _xmlString);
}
if (!_coffeeString) {
_coffeeString = [[NSMutableString alloc] initWithString:string];
}
else {
_coffeeString = string;
}
if (coffeeArray == NULL) {
NSLog(@"Stick1 the coffeeArray was null");
coffeeArray = [[NSMutableArray alloc] init];
}
//add the returned string into an array
[coffeeArray addObject:_coffeeString];
NSLog(@"Stick1 array %@", coffeeArray);
// parse the returned data in array
NSString *seperate = [coffeeArray componentsJoinedByString:@"\n"];
NSLog(@"Stick1 seperate is %@", seperate);
// another parsing
NSArray* words = [seperate componentsSeparatedByCharactersInSet :[NSCharacterSet whitespaceAndNewlineCharacterSet]];
//or use newLineCharecterSet
// Take out all of the whitespace like tabs and spaces and new lines
_singleName = [words componentsJoinedByString:@""];
NSLog(@"Stick1 final result %@", _singleName);
}
基本上,它被稱爲兩次,但並沒有顯示這兩組數據。一旦找到咖啡,它會顯示咖啡的第一部分(最小值),但不顯示咖啡的第二個實例(最大值)。
哪裏是你的'解析器:didEndElement:'方法? – mbm29414 2015-01-26 18:15:52
@ mbm29414我的didEndElement是空的。 – John 2015-01-26 18:18:50
你想要輸出什麼數據? – mbm29414 2015-01-26 18:30:48