1
我有很多圖片和實況內容的HTML網頁。我需要parse the data from the webpage(HTML)
並在iPhone應用程序中顯示。我正在使用下面的代碼來解析HTML內容。但我不知道如何解析標籤中的子標籤?如何在iPhone應用程序中解析HTML子標籤?
{
NSURL *url = [NSURL URLWithString:@"http://www.samplewebpage.com/vd/t/1/830.html"];
NSData *data = [[NSData alloc] initWithContentsOfURL:url];
NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"Response : %@", responseString);
NSMutableArray *imageURLArray = [[NSMutableArray alloc] init];
NSMutableArray *divClassArray = [[NSMutableArray alloc] init];
//NSString *regexStr = @"<A HREF=\"([^>]*)\">";
// For image : 1. img src=\"([^>]*)\" 2. <img src=\"([^>]*)\">
// For getting Class div class
NSString *regularExpressString = @"div class=\"([^>]*)\"";
NSError *error;
NSInteger i =0;
while (i<[responseString length])
{
NSRegularExpression *testRegularExpress = [NSRegularExpression regularExpressionWithPattern:regularExpressString options:NSRegularExpressionCaseInsensitive error:&error];
if(testRegularExpress == nil)
{
NSLog(@"Error making regex: %@", error);
}
NSTextCheckingResult *textCheckingResult = [testRegularExpress firstMatchInString:responseString options:0 range:NSMakeRange(i, [responseString length]-i)];
NSRange range = [textCheckingResult rangeAtIndex:1];
if (range.location == 0)
{
break;
}
NSString *classNameString = [responseString substringWithRange:range];
NSLog(@"Div Class Name : %@", classNameString);
[divClassArray addObject:classNameString];
i= range.location;
//NSLog(@"Range.location : %i",range.location);
i=i+range.length;
}
NSLog(@"divClass Array : %@, Count : %d", divClassArray, [divClassArray count]);
}
響應:
<div class="phoneModelItems" style="width:30%;margin-right:4px;"><a href="javascript:noxLatestChart.navToLatestChart('latest');" style="font-weight:italic;">Nokia Model</a></div>
我想從類phoneModelItems獲取文本諾基亞型號。你能告訴我如何檢索文本'諾基亞模型'?提前致謝。
你可以張貼responseString您的回覆 – CarlJ
Mr.Meccan感謝。我編輯了這個問題。我已經提到了這個迴應。你能幫我麼?謝謝。 – Gopinath
@Gopinath你有解決方案嗎? –