【问题标题】:HTML Parsing in iOSiOS 中的 HTML 解析
【发布时间】:2014-09-30 05:43:40
【问题描述】:

我想从数据库中检索 html 数据。我用这段代码来解析,

  -(NSString *)MultipleCharactersHTML:(NSString *)url
 {
NSMutableURLRequest *request =[[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:url]];
NSData *returnData = [ NSURLConnection sendSynchronousRequest:request returningResponse: nil error: nil ];

NSString *returnString = [[NSString alloc]initWithData:returnData encoding:NSUTF8StringEncoding];
//  NSLog(@"return string %@",returnString);
returnString=[returnString stringByReplacingOccurrencesOfString:@"\\" withString:@""];
returnString=[returnString stringByReplacingOccurrencesOfString:@"\"<" withString:@"<"];
returnString=[returnString stringByReplacingOccurrencesOfString:@">\"" withString:@">"];
NSError *err = nil;
// NSLog(@"return string replaced %@",returnString);

//    NSMutableArray *search = [NSJSONSerialization JSONObjectWithData:[returnString dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:&err];
//  NSLog(@"search response %@",search);
return returnString;
 }

但我无法检索数据...

我要将此数据设置为我的 webview 的 htmlstring...请任何人指导我..

数据库中的数据将采用这种格式...

  <div class="layout-content-row content-header " style="display: inline-block; margin: 36px 0px 23px; height: 30px; padding: 0px 0px 10px; color: rgb(34, 34, 34); font-family: Roboto, sans-serif; font-size: 14px; line-height: 19px; background-color: rgb(249, 249, 249);"><div class="layout-content-col span-9" style="float: left; margin-left: 0px; width: 520px; height: 1px;"><h1 itemprop="name" style="color: rgb(51, 51, 51); font-size: 34px; margin-top: 0px; margin-bottom: 0px; padding: 0px; line-height: 30px; width: 700px;">Creative Vision</h1></div><div class="paging-links layout-content-col span-4" itemscope="" itemtype="http://schema.org/SiteNavigationElement" style="float: left; margin-left: 20px; position: relative; height: 30px; width: 220px;"><a href="http://developer.android.com/design/index.html" class="prev-page-link" zh-tw-lang="&#19978;&#19968;&#22530;&#35506;" zh-cn-lang="&#19978;&#19968;&#35838;" ru-lang="&#1055;&#1088;&#1077;&#1076;&#1099;&#1076;&#1091;&#1097;&#1080;&#1081;" ko-lang="&#51060;&#51204;" ja-lang="&#21069;&#12408;" es-lang="Anterior" style="position: absolute; color: rgb(37, 138, 175); text-transform: uppercase; left: -15px;">PREVIOUS</a><a href="http://developer.android.com/design/get-started/principles.html" class="next-page-link" zh-tw-lang="&#19979;&#19968;&#22530;&#35506;" zh-cn-lang="&#19979;&#19968;&#35838;" ru-lang="&#1057;&#1083;&#1077;&#1076;&#1091;&#1102;&#1097;&#1080;&#1081;" ko-lang="&#45796;&#51020;" ja-lang="&#27425;&#12408;" es-lang="Siguiente" style="position: absolute; color: rgb(37, 138, 175); text-transform: uppercase; right: 0px;">NEXT</a></div></div><span style="color: rgb(34, 34, 34); font-family: Roboto, sans-serif; font-size: 14px; line-height: 19px; background-color: rgb(249, 249, 249);"></span><div id="jd-content" style="height: 790px; color: rgb(34, 34, 34); font-family: Roboto, sans-serif; font-size: 14px; line-height: 19px; background-color: rgb(249, 249, 249);"><div class="jd-descr" itemprop="articleBody" style="height: 725px;"><img src="http://developer.android.com/design/media/creative_vision_main.png" style="max-width: 100%; margin-bottom: 15px;"><div class="vspace size-1" style="height: 10px;"> </div><p itemprop="description" style="margin-bottom: 15px;">Starting with Ice Cream Sandwich, we focused the design of Android around these three overarching goals, which apply to our core apps as well as the system at large. As you work with Android, consider these goals.</p><div class="vspace size-1" style="height: 10px;"> </div><div class="layout-content-row" style="display: inline-block; margin-bottom: 10px;"><div class="layout-content-col span-4" style="float: left; margin-left: 0px; width: 220px;"><h4 style="line-height: 21px; margin: 0px 0px 12px;">Enchant me</h4><p style="margin-bottom: 15px;">Beauty is more than skin deep. Android apps are sleek and aesthetically pleasing on multiple levels. Transitions are fast and clear; layout and typography are crisp and meaningful. App icons are works of art in their own right. Just like a well-made tool, your app should strive to combine beauty, simplicity and purpose to create a magical experience that is effortless and powerful.</p></div><div class="layout-content-col span-5" style="float: left; margin-left: 20px; width: 280px;"><h4 style="line-height: 21px; margin: 0px 0px 12px;">Simplify my life</h4><p style="margin-bottom: 15px;">Android apps make life easier and are easy to understand. When people use your app for the first time, they should intuitively grasp the most important features. The design work doesn't stop at the first use, though. Android apps remove ongoing chores like file management and syncing. Simple tasks never require complex procedures, and complex tasks are tailored to the human hand and mind. People of all ages and cultures feel firmly in control, and are never overwhelmed by too many choices or irrelevant flash.</p></div><div class="layout-content-col span-4" style="float: left; margin-left: 20px; width: 220px;"><h4 style="line-height: 21px; margin: 0px 0px 12px;">Make me amazing</h4><p style="margin-bottom: 15px;">It's not enough to make an app that is easy to use. Android apps empower people to try new things and to use apps in inventive new ways. Android lets people combine applications into new workflows through multitasking, notifications, and sharing across apps. At the same time, your app should feel personal, giving people access to superb technology with clarity and grace.</p></div></div></div><div class="content-footer layout-content-row" itemscope="" itemtype="http://schema.org/SiteNavigationElement" style="display: inline-block; margin-bottom: 10px; border-top-width: 1px; border-top-style: solid; border-top-color: rgb(204, 204, 204); margin-top: 10px; padding-top: 10px; width: 760px;"><div class="paging-links layout-content-col col-10" style="float: left; margin-left: 0px; position: relative; height: 30px; display: inline; margin-right: 10px; width: 580px;"><a href="http://developer.android.com/design/get-started/principles.html" class="next-page-link" zh-tw-lang="&#19979;&#19968;&#22530;&#35506;" zh-cn-lang="&#19979;&#19968;&#35838;" ru-lang="&#1057;&#1083;&#1077;&#1076;&#1091;&#1102;&#1097;&#1080;&#1081;" ko-lang="&#45796;&#51020;" ja-lang="&#27425;&#12408;" es-lang="Siguiente" style="position: absolute; color: rgb(37, 138, 175); text-transform: uppercase; right: 0px; left: 0px;">NEXT: DESIGN PRINCIPLES</a></div></div></div>  

【问题讨论】:

  • 你能告诉我我的帖子有什么问题吗?

标签: ios json parsing webview


【解决方案1】:

是的... 正如 Harshini Nerella 所说,您可以使用 UIWebView 直接从数据库加载标签。并确保您以正确的格式将数据保存在数据库中。不要在 PHP 文件中对检索到的值进行编码。

【讨论】:

    【解决方案2】:

    如果您得到的响应是 htmlstring,那么您可以将该 html 字符串直接设置为 UIWebView 实例。

     UIWebView *eventInfoWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 130, self.view.frame.size.width, eventInfoSV.frame.size.height-130)];
     [eventInfoWebView loadHTMLString:htmlString baseURL:nil];
    

    【讨论】:

    • NSData *returnData = [ NSURLConnection sendSynchronousRequest:request returnedResponse: nil error: nil ];此行未将数据转换为字符串...
    【解决方案3】:

    使用第三方类来解析 HTML 代码,特别是解析每个标签..

    例子:

    https://github.com/zootreeves/Objective-C-HMTL-Parser

    https://github.com/mwaterfall/MWFeedParser

    【讨论】:

      猜你喜欢
      • 2015-09-27
      • 2013-04-01
      • 1970-01-01
      • 2013-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-13
      相关资源
      最近更新 更多