2009-07-31 24 views
0

我有以下文字NSScanner環流式檢索多個值

<select name="username"><option value="177"> Bob1 
       </option><option value="221"> Bob2 
       </option><option value="227"> Bob3 
       </option><option value="164"> Bob4 
       </option><option value="271"> Bob5 
       </option><option value="137"> Bob6 
       </option><option value="105"> Bob7 
       </option><option value="285"> Bob8 
       </option><option value="281"> Bob9 
       </option><option value="265"> Bob10 
       </option></select> 

,我試圖用一個NSScanner得到選項標籤中的選項值和名稱。到目前爲止,我有以下代碼

for (int y = 1; y < 16; y++) { 
    NSScanner *scanner1 = [NSScanner scannerWithString:htmlsource]; 
    [scanner1 scanUpToString:[NSString stringWithFormat:@"<option value=\""] intoString:NULL]; 
    [scanner1 scanString:[NSString stringWithFormat:@"<option value=\""] intoString:NULL]; 
    [scanner1 scanUpToString:@"\"" intoString:&result]; 
    NSLog(@"%i",[scanner1 scanLocation]); 
    NSLog(result); 

    [scanner1 setScanLocation:([scanner1 scanLocation] - 18)]; 
    [scanner1 scanUpToString:[NSString stringWithFormat:@"<option value=\"%@\">",result] intoString:NULL]; 
    [scanner1 scanString:[NSString stringWithFormat:@"<option value=\"%@\">",result] intoString:NULL]; 
    [scanner1 scanUpToString:@"</option>" intoString:&result]; 
    //NSLog([NSString stringWithFormat:@"<option value=\"%@\">",result]); 
    NSLog(@"%i",[scanner1 scanLocation]); 
    NSLog(result); 

    } 

這隻適用於第一個條目。我該怎麼解決這個問題呢?還是必須從停止的地方開始掃描?如果是這樣的話? 結果至今都..

2009-07-31 08:15:53.859 App1[1000:20b] 683 
2009-07-31 08:15:53.860 App1[1000:20b] 177 
2009-07-31 08:15:53.860 App1[1000:20b] 712 
2009-07-31 08:15:53.860 App1[1000:20b] Bob1 

2009-07-31 08:15:53.861 App1[1000:20b] 683 
2009-07-31 08:15:53.861 App1[1000:20b] 177 
2009-07-31 08:15:53.862 App1[1000:20b] 712 
2009-07-31 08:15:53.862 App1[1000:20b] Bob1 

回答

1

總是有RegexKitLite

這個版本保持<option>...</option>內的空白:

NSString *regex = @"(?si)<option\\s+value\\s*=\\s*\"([^\"]*)\"[^>]*>(.*?)</option>"; 
NSArray *htmlOptionsArray = [htmlsource arrayOfCaptureComponentsMatchedByRegex:regex]; 
for(NSArray *parsedOptionArray in htmlOptionsArray) { 
    NSString *value = [parsedOptionArray objectAtIndex:1UL]; 
    NSString *text = [parsedOptionArray objectAtIndex:2UL]; 
    NSLog(@"Value: '%@', text: '%@'", value, text); 
} 

輸出示例:

2009-07-31 04:20:23.692 so[35423:807] Value: '177', text: ' Bob1 
       ' 
2009-07-31 04:20:23.699 so[35423:807] Value: '221', text: ' Bob2 
       ' 
.... 
2009-07-31 04:20:23.725 so[35423:807] Value: '281', text: ' Bob9 
       ' 
2009-07-31 04:20:23.726 so[35423:807] Value: '265', text: ' Bob10 
       ' 

這個版本剝掉,在期權文本任何額外的空格:

NSString *regex = @"(?si)<option\\s+value\\s*=\\s*\"([^\"]*)\"[^>]*>\\s*(.*?)\\s*</option>"; 
NSArray *htmlOptionsArray = [htmlsource arrayOfCaptureComponentsMatchedByRegex:regex]; 
for(NSArray *parsedOptionArray in htmlOptionsArray) { 
    NSString *value = [parsedOptionArray objectAtIndex:1UL]; 
    NSString *text = [parsedOptionArray objectAtIndex:2UL]; 
    NSLog(@"Value: '%@', text: '%@'", value, text); 
} 

示例輸出:

2009-07-31 04:21:50.352 so[35436:807] Value: '177', text: 'Bob1' 
2009-07-31 04:21:50.354 so[35436:807] Value: '221', text: 'Bob2' 
... 
2009-07-31 04:21:50.359 so[35436:807] Value: '281', text: 'Bob9' 
2009-07-31 04:21:50.359 so[35436:807] Value: '265', text: 'Bob10' 
0

如果它是格式良好的XML,那麼你可能會更好過使用類似NSXML XML解析器做繁重的你:

NSXML

另一個問題是,您將掃描儀重置爲選項值字符串的開始位置,因此,當您重新掃描時,您將從每次停用的位置開始。其實這一點實際上不是這樣做,並繼續前進?

[scanner1 setScanLocation:([scanner1 scanLocation] - 18)]; 

如果你評論該行,它是否神奇地開始工作?