iphone 把HTML页面存到字符串中~用uiwebView解析~

-(void)viewDidLoad{
	NSString *htmlContent = @"<div style =\" font-family:Helvetica,Arial,sans-serif;font-size:20pt;\" align=\"center\">";
	NSMutableString *htmlPage = [NSMutableString new];
	[htmlPage appendString:htmlContent];
	[htmlContent release];

	[ htmlPage appendString:@"视频监控信息统计"];
	[ htmlPage appendString:@"<p>"];
	[ htmlPage appendString:@"</ span"];
	[ htmlPage appendString:@"<table cellspacing=0 cellpadding=3 border=1>"];//cellpadding
	
	[ htmlPage appendString:@"<tr>"];
		[ htmlPage appendString:@"<th>"];
			[ htmlPage appendString:@"节目名"];
		[ htmlPage appendString:@"</th>"];
		[ htmlPage appendString:@"<th>"];
			[ htmlPage appendString:@"系统层"];
		[ htmlPage appendString:@"</th>"];
		[ htmlPage appendString:@"<th>"];
			[ htmlPage appendString:@"视频层"];
		[ htmlPage appendString:@"</th>"];
		[ htmlPage appendString:@"<th>"];
			[ htmlPage appendString:@"音频层"];
		[ htmlPage appendString:@"</th>"];
	[ htmlPage appendString:@"</tr>"];


	[ htmlPage appendString:@"<tr>"];
	//利用HTTPRequest请求Internet上要解析的XML文件 
	
	NSURL *url = [NSURL URLWithString:@"http://192.168.1.112/tsalog/xmls.aspx"]; //请求的URL	
	NSMutableURLRequest* request = [NSMutableURLRequest new];    //创建请求	
	[request setURL:url]; //把URL放在请求中   	
	[request setHTTPMethod:@"GET"]; //请求的方式	
	NSHTTPURLResponse* response;  //响应请求  
	NSData* data = [NSURLConnection sendSynchronousRequest:request    
					
										 returningResponse:&response 
					
													 error:nil]; // 响应的数据传给NSData
	NSString* responseXMLResult = [[NSString alloc] initWithData:data 
														encoding: CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingUTF8)]; // 响应的XML结果存储到string中 
	NSError *error;
	GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithXMLString: responseXMLResult
																options:0 error:&error];    // 把xml结果传给GDataxmldocument进行解析
	if (doc == nil) { return; }  // 判断解析文档是否为空
	NSLog(@"LOG=%@", [[NSString alloc] initWithData:doc.XMLData encoding:NSUTF8StringEncoding]); //日志输出
	NSArray *documentElements = [doc.rootElement nodesForXPath:@"//DocumentElement/tbl_Errors" error:nil];  //xml成员放在数组中
	
	
	for (GDataXMLElement *partyMember in documentElements) {
		
		NSString *_pgmname;// 存储解析后 节目的字符串
		NSString *_systemCount;		//系统报警
		NSString *_videosCount;		//视频报警
		NSString *_audiosCount;		//音频报警
		
		// Pgmname  节目名
		[ htmlPage appendString:@"<td>"];
		NSArray *pgmnames = [partyMember elementsForName:@"pgmname"]; // 存储所有的 XML中 <pgmname></pgmname>节点
		if (pgmnames.count > 0) { 
			GDataXMLElement *firstPgmname = (GDataXMLElement *)[pgmnames objectAtIndex:0];
			_pgmname = firstPgmname.stringValue;
		} else continue;
		
		NSLog(@"_pgmname=%@",_pgmname); //输出到控制台
		[NSString stringWithFormat:_pgmname]; 
		
		[ htmlPage appendString:_pgmname];

		[ htmlPage appendString:@"</td>"];
		
		
		[ htmlPage appendString:@"<td>"];	
		// systemCount  系统报警 		
		NSArray *systemCounts = [partyMember elementsForName:@"systemcount"]; // 存储所有的 XML中 <pgmname></pgmname>节点
		if (systemCounts.count > 0) { 
			GDataXMLElement *firstSystemCount = (GDataXMLElement *)[systemCounts objectAtIndex:0];
			_systemCount = firstSystemCount.stringValue;
		} else continue;
		NSLog(@"_systemCount=%@",_systemCount); //输出到控制台
		[NSString stringWithFormat:_systemCount];
		
		if ([_systemCount isEqualToString:@"0"]) {
						[ htmlPage appendString:@"<span style=\" border:solid 1px green;background-color:lightgreen;width:8px;height:8px;line-height:8px;display:inline-block;\">"]; 
						[ htmlPage appendString:@"</span>"];

		}else {
						[ htmlPage appendString:@"<span style=\" border:solid 1px orchid;background-color:fuchsia;width:8px;height:8px;line-height:8px;display:inline-block;\">"]; 
						[ htmlPage appendString:@"</span>"];

		}
		[ htmlPage appendString:@" "];
		[ htmlPage appendString:_systemCount];
		[ htmlPage appendString:@"</td>"];
		
		
		[ htmlPage appendString:@"<td>"];
		// videoCount   视频报警
		NSArray *videosCounts = [partyMember elementsForName:@"videoscount"];
		if (videosCounts.count > 0) {
			GDataXMLElement *firstVideosCount = (GDataXMLElement*)[videosCounts objectAtIndex:0];
			_videosCount = firstVideosCount.stringValue;
		} else continue;
		NSLog(@"_videosCount=%@",_videosCount);
		[NSString stringWithFormat:_videosCount];
		
		if ([_videosCount isEqualToString:@"0"]) {
			[ htmlPage appendString:@"<span style=\" border:solid 1px green;background-color:lightgreen;width:8px;height:8px;line-height:8px;display:inline-block;\">"]; 
			[ htmlPage appendString:@"</span>"];
			
		}else {
			[ htmlPage appendString:@"<span style=\" border:solid 1px orchid;background-color:fuchsia;width:8px;height:8px;line-height:8px;display:inline-block;\">"]; 
			[ htmlPage appendString:@"</span>"];
			
		}
		[ htmlPage appendString:@" "];
		
		[ htmlPage appendString:_videosCount];
		[ htmlPage appendString:@"</td>"];
		
		
		
		
		[ htmlPage appendString:@"<td>"];
		// audiosCount 音频报警 
		NSArray *audiosCounts = [partyMember elementsForName:@"audioscount"];
		if (audiosCounts.count > 0) {
			GDataXMLElement *firstAudiosCount = (GDataXMLElement*)[audiosCounts objectAtIndex:0];
			_audiosCount = firstAudiosCount.stringValue;
		} else continue;
		NSLog(@"_audiosCount=%@",_audiosCount);
		
		[NSString stringWithFormat:_audiosCount];
		
		if ([_audiosCount isEqualToString:@"0"]) {
			[ htmlPage appendString:@"<span style=\" border:solid 1px green;background-color:lightgreen;width:8px;height:8px;line-height:8px;display:inline-block;\">"]; 
			[ htmlPage appendString:@"</span>"];
			
		}else {
			[ htmlPage appendString:@"<span style=\" border:solid 1px orchid;background-color:fuchsia;width:8px;height:8px;line-height:8px;display:inline-block;\">"]; 
			[ htmlPage appendString:@"</span>"];
			
		}
		[ htmlPage appendString:@" "];
		
		[ htmlPage appendString:_audiosCount];
		[ htmlPage appendString:@"</td>"];
		
		
		[ htmlPage appendString:@"</tr>"];

	}
	
	[url release]; //释放内存
	
	[doc release];// 释放doc
	
	[ htmlPage appendString:@"</table>"];
	[ htmlPage appendString:@"</ span"];

	[webView loadHTMLString:htmlPage baseURL:nil];
	[htmlPage release];
	[super viewDidLoad];
}


你可能感兴趣的:(iphone 把HTML页面存到字符串中~用uiwebView解析~)