NSXMLParser를 사용하여이 XML을 구문 분석하려고합니다. 내가 지금처럼 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);
}
기본적으로이 두 번 호출되는,하지만 두 세트의 데이터를 표시하지 않습니다. 커피를 발견하면 커피의 첫 번째 부분 (최소)을 표시하지만 두 번째 커피 (최대)를 표시하지 못합니다.
입니까? – mbm29414
@ mbm29414 내 didEndElement가 비어 있습니다. – John
원하는 데이터 출력은 무엇입니까? – mbm29414