我正在尝试向我的应用程序添加导入书签功能。我有一些,但它只会提取所有 URL 和标题。
- (NSArray *)urlsInHTML:(NSString *)html {
NSError *error;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(?<=href=\").*?(?=\")" options:NSRegularExpressionCaseInsensitive error:&error];
NSArray *arrayOfAllMatches = [regex matchesInString:html options:0 range:NSMakeRange(0, [html length])];
NSMutableArray *arrayOfURLs = [[NSMutableArray alloc] init];
for (NSTextCheckingResult *match in arrayOfAllMatches) {
NSString* substringForMatch = [html substringWithRange:match.range];
NSLog(@"Extracted URL: %@",substringForMatch);
[arrayOfURLs addObject:substringForMatch];
}
// return non-mutable version of the array
return [NSArray arrayWithArray:arrayOfURLs];
}
- (NSArray *)titlesOfTagsInHTML:(NSString *)html {
NSError *error;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(?<=\"\\>)(.*?)(?=\\<\\/)" options:NSRegularExpressionCaseInsensitive error:&error];
NSArray *arrayOfAllMatches = [regex matchesInString:html options:0 range:NSMakeRange(0, [html length])];
NSMutableArray *arrayOfURLs = [[NSMutableArray alloc] init];
for (NSTextCheckingResult *match in arrayOfAllMatches) {
NSString* substringForMatch = [html substringWithRange:match.range];
NSLog(@"Extracted Title: %@",substringForMatch);
[arrayOfURLs addObject:substringForMatch];
}
// return non-mutable version of the array
return [NSArray arrayWithArray:arrayOfURLs];
}
- (IBAction)import {
ProgressAlertView *progressAlert = [[ProgressAlertView alloc] initWithTitle:@"Crux" message:@"Importing Bookmarks..." delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
[progressAlert show];
NSString *htmlString = [NSString stringWithContentsOfFile:importingBookmarkFilePath encoding:NSUTF8StringEncoding error:nil];
NSArray *urls = [self urlsInHTML:htmlString];
NSArray *titles = [self titlesOfTagsInHTML:htmlString];
//float progress = [[NSNumber numberWithInt:i] floatValue]/[[NSNumber numberWithInteger:[urls count]-1] floatValue];
for (int i=0; i<[urls count]; i++) {
Bookmark *importedBookmark = [[Bookmark alloc] init];
importedBookmark.url = urls[i];
importedBookmark.title = titles[i];
[[[BookmarkManager sharedInstance] bookmarks] addObject:importedBookmark];
[[BookmarkManager sharedInstance] saveBookmarks];
}
}
但我找不到如何确定文件夹,所以我可以完全按照它们在其他浏览器中的方式保留主题。要查看 safari 如何导出它们,只需转到文件>导出书签,您就可以看到 html 文件。它将所有内容放在带有文件夹标题的定义列表中。使用 NSREgularExpression 或其他方式,我如何获取每个文件夹标题以及该文件夹中的所有内容?
我曾尝试使用 NSXMLParser 解析 html,但它在第一个定义列表标记处停止并失败。