2015-01-26 65 views
0

我想解析這個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); 
} 

基本上,它被稱爲兩次,但並沒有顯示這兩組數據。一旦找到咖啡,它會顯示咖啡的第一部分(最小值),但不顯示咖啡的第二個實例(最大值)。

+0

哪裏是你的'解析器:didEndElement:'方法? – mbm29414 2015-01-26 18:15:52

+0

@ mbm29414我的didEndElement是空的。 – John 2015-01-26 18:18:50

+0

你想要輸出什麼數據? – mbm29414 2015-01-26 18:30:48

回答

1

我認爲你有幾個問題正在進行。一個是(至少使用提供的XML),<Coffee>...</Coffee>定義了一個完整的XML文檔,所以你可能會得到一個未處理的解析錯誤。你應該執行下面的代碼在你的NSXMLParserDelegate,看報道有什麼問題:

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError { 
    NSLog(@"error: %@", parseError); 
} 
- (void)parser:(NSXMLParser *)parser validationErrorOccurred:(NSError *)validationError { 
    NSLog(@"error: %@", validationError); 
} 

你還必須在那裏你試圖關閉基於標籤的名字在你的代碼來設置一個觸發/標誌問題(elementName)的節點,但實際上您關心的是包含在子節點中的數據。

此外,parser:foundCharacters:保證返回標籤的所有內容在一個字符串,所以你最好處理parser:didStartElement:...parser:didEndElement:...了很多。然後,在parser:foundCharacters:中,您可以確保您位於適當的節點並且附加已找到的字符。

下面是一些示例代碼,您開始基於數據您提供獲得:

注:我只是採取了一槍你真正想要的,因爲你的數據是有點模糊。另外,我在全新的單視圖應用程序的應用程序代表中完成了此操作。這是不是真正實現的方法!

// AppDelegate.m 
#import "AppDelegate.h" 
@interface AppDelegate() <NSXMLParserDelegate> { 

} 
@property (assign, nonatomic) BOOL    inNode; 
@property (strong, nonatomic) NSMutableArray *coffeeArray; 
@property (strong, nonatomic) NSMutableString *coffeeString; 
@property (copy , nonatomic) NSString   *singleName; 
@property (copy , nonatomic) NSString   *xmlString; 
@end 
@implementation AppDelegate 
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
    NSXMLParser *parser = [[NSXMLParser alloc] initWithData:[[self xml] dataUsingEncoding:NSUTF8StringEncoding]]; 
    parser.delegate  = self; 
    [parser parse]; 
    return YES; 
} 
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict { 
    NSLog(@"Fired Stick1"); 

    self.xmlString = elementName; 

    if ([self.xmlString isEqualToString:@"Coffee"]) { 
     NSLog(@"Stick1 Coffee:"); 
     self.coffeeString = [[NSMutableString alloc] init]; 
     self.inNode   = YES; 
    } else if ([self.xmlString isEqualToString:@"Tea"]) { 
     NSLog(@"Stick1 Tea:"); 
     self.coffeeString = [[NSMutableString alloc] init]; 
     self.inNode   = YES; 
    } else { 
     self.inNode   = NO; 
    } 
} 
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { 
    if (self.inNode == YES) { 
     if ([self.xmlString isEqualToString:@"Coffee"] || [self.xmlString isEqualToString:@"Tea"]) { 
      NSLog(@"%@ Called", self.xmlString); 
     } 
     [self.coffeeString appendString:string]; 
    } 
} 
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName { 
    if ([elementName isEqualToString:@"Coffee"] || [elementName isEqualToString:@"Tea"]) { 
     if (self.coffeeArray == nil) { 
      NSLog(@"Stick1 the coffeeArray was null"); 
      self.coffeeArray = [[NSMutableArray alloc] init]; 
     } 

     //add the returned string into an array 
     [self.coffeeArray addObject:self.coffeeString]; 
     NSLog(@"Stick1 array %@", self.coffeeArray); 

     // parse the returned data in array 
     NSString *seperate = [self.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); 

    } 
} 
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError { 
    NSLog(@"error: %@", parseError); 
} 
- (void)parser:(NSXMLParser *)parser validationErrorOccurred:(NSError *)validationError { 
    NSLog(@"error: %@", validationError); 
} 
- (NSString *)xml { 
    NSMutableString *xml = [[NSMutableString alloc] init]; 
    [xml appendString:@"<Document>"]; 
    [xml appendString:@"" 
    @"<Coffee>" 
    @" <Type Name=\"Type\">" 
    @" Minimum Drinking Amount" 
    @" </Type>" 
    @"</Coffee>" 
    @"<Coffee>" 
    @" <Type Name=\"Type\">" 
    @" Maximum Drinking Amount" 
    @" </Type>" 
    @"</Coffee>\n"]; 
    [xml appendString:@"" 
    @"<Coffee>" 
    @" <Type Name=\"Type\">" 
    @" Minimum Drinking Amount" 
    @" </Type>" 
    @"</Coffee>" 
    @"<Coffee>" 
    @" <Type Name=\"Type\">" 
    @" Maximum Drinking Amount" 
    @" </Type>" 
    @"</Coffee>\n"]; 
    [xml appendString:@"" 
    @"<Coffee>" 
    @" <Type Name=\"Type\">" 
    @" Minimum Drinking Amount" 
    @" </Type>" 
    @"</Coffee>" 
    @"<Coffee>" 
    @" <Type Name=\"Type\">" 
    @" Maximum Drinking Amount" 
    @" </Type>" 
    @"</Coffee>\n"]; 
    [xml appendString:@"" 
    @"<Coffee>" 
    @" <Type Name=\"Type\">" 
    @" Minimum Drinking Amount" 
    @" </Type>" 
    @"</Coffee>" 
    @"<Coffee>" 
    @" <Type Name=\"Type\">" 
    @" Maximum Drinking Amount" 
    @" </Type>" 
    @"</Coffee>\n"]; 
    [xml appendString:@"" 
    @"<Coffee>" 
    @" <Type Name=\"Type\">" 
    @" Minimum Drinking Amount" 
    @" </Type>" 
    @"</Coffee>" 
    @"<Coffee>" 
    @" <Type Name=\"Type\">" 
    @" Maximum Drinking Amount" 
    @" </Type>" 
    @"</Coffee>\n"]; 
    [xml appendString:@"" 
    @"<Coffee>" 
    @" <Type Name=\"Type\">" 
    @" Minimum Drinking Amount" 
    @" </Type>" 
    @"</Coffee>" 
    @"<Coffee>" 
    @" <Type Name=\"Type\">" 
    @" Maximum Drinking Amount" 
    @" </Type>" 
    @"</Coffee>\n"]; 
    [xml appendString:@"</Document>"]; 
    return [xml copy]; 
} 
@end 
相關問題