解析的方式 有俩种 sax dom 里面都涉及到了
但是sax有个缺陷---中文和英文在同时在一个字符串的时候他分成了俩个部分。我也很奇怪
dom是本局我自己的情况来的
(希望有兴趣的朋友们研究下反馈给我 谢谢)
#import <Foundation/Foundation.h>
#import "TBXML.h"
@protocol MyProtocol <NSObject>
-(void)kankan:(NSArray*)f;
-(void)kankan1:(NSArray*)f;
-(void)kankan2:(NSArray*)f;
-(void)kankan3:(NSArray*)f;
@end
@interface TabelViewShow :NSObject<NSXMLParserDelegate,NSURLConnectionDelegate>{
NSString*str;
id<MyProtocol> _delegate;
NSString *currentString;
NSString *attrString;
NSString *preAttrString;
}
-(void)show:(NSArray*)array;
-(void)parse:(NSData*)path;
@property(nonatomic,retain)id<MyProtocol>delegate;
@property (strong,nonatomic) NSURLConnection *conn;
) NSMutableData *webData;
) NSXMLParser *xmlParser;
) NSMutableArray *soapResults;
@property (nonatomic)BOOL elementFound;
@property (strong,nonatomic) NSString *matchingElement;
@end
//
// TabelViewShow.m
// demo5
// Created by chengs on 13-5-8.
// Copyright (c) 2013年 Chengs. All rights reserved.
//
#import "TabelViewShow.h"
@implementation TabelViewShow
@synthesize elementFound;
@synthesize matchingElement;
@synthesize soapResults;
@synthesize delegate =_delegate;
-(void)show:(NSArray*)array{
matchingElement = [array objectAtIndex:0];
NSString*paw = [array objectAtIndex:1];
if (!soapResults) {
soapResults = [[NSMutableArrayalloc] init];
}
NSString *soapMsg = [NSStringstringWithFormat:
@"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
"<soap12:Envelope "
"xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
"xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" "
"xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">"
"<soap12:Body>"
"%@"
"</soap12:Body>"
"</soap12:Envelope>",paw];
NSLog(@"tableviewshow = %@",soapMsg);
NSURL *url = [NSURLURLWithString:@"http://xxx.xxx.xxx.xxx:xxx/WellService.asmx"];
// 根据上面的URL创建一个请求
NSMutableURLRequest *req = [NSMutableURLRequestrequestWithURL:url];
NSString *msgLength = [NSStringstringWithFormat:@"%d",[soapMsglength]];
// 添加请求的详细信息,与请求报文前半部分的各字段对应
[req addValue:@"application/soap+xml; charset=utf-8"forHTTPHeaderField:@"Content-Type"];
[req addValue:msgLengthforHTTPHeaderField:@"Content-Length"];
// 设置请求行方法为POST,与请求报文第一行对应
[req setHTTPMethod:@"POST"];
// 将SOAP消息加到请求中
[req setHTTPBody: [soapMsgdataUsingEncoding:NSUTF8StringEncoding]];
// 创建连接
_conn = [[NSURLConnectionalloc] initWithRequest:reqdelegate:self];
if (_conn) {
_webData = [[NSMutableDataalloc]init];
}
}
// 刚开始接受响应时调用
-(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *) response{
NSLog(@"开始接受");
[_webDatasetLength: 0];
}
// 每接收到一部分数据就追加到webData中
-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *) data {
NSLog(@"受到了数据");
[_webDataappendData:data];
}
// 出现错误时
-(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *) error {
NSLog(@"====%@",error);
_conn = nil;
_webData = nil;
}
// 完成接收数据时调用
-(void) connectionDidFinishLoading:(NSURLConnection *) connection {
NSLog(@"接收完成");
NSString *theXML = [[NSStringalloc] initWithBytes:[_webDatamutableBytes]
length:[_webDatalength]
encoding:NSUTF8StringEncoding];
// NSLog(@"=======%@",theXML);
NSData *newData = [theXMLdataUsingEncoding:NSUTF8StringEncoding];
[selfparse:newData];
// [self SoapMessageResultXml:newData ServiceMethodName:matchingElement];
// // 使用NSXMLParser解析出我们想要的结果
// NSData *newData = [theXML dataUsingEncoding:NSUTF8StringEncoding];
// _xmlParser = [[NSXMLParser alloc] initWithData: newData];
// _xmlParser.delegate = self;
// [_xmlParser setShouldResolveExternalEntities: YES];
// [_xmlParser parse];
}
-(void)parse:(NSData*)path{
NSError *err;
str = [NSStringstringWithFormat:@"%@Response",matchingElement];
NSString*Result = [NSStringstringWithFormat:@"%@Result",matchingElement];
TBXML *tbXML = [TBXMLnewTBXMLWithXMLData:path error:&err];
if (!tbXML) {
NSLog(@"%@",[errlocalizedDescription]);
}
TBXMLElement *rootElement = tbXML.rootXMLElement;
if (rootElement) {
TBXMLElement *bookElement = [TBXMLchildElementNamed:@"soap:Body"parentElement:rootElement];
if (bookElement) {
TBXMLElement *getUserCheckResponse = [TBXMLchildElementNamed:strparentElement:bookElement];
if (getUserCheckResponse) {
TBXMLElement *getUserCheckResult = [TBXMLchildElementNamed:Result parentElement:getUserCheckResponse];
if (getUserCheckResult) {
TBXMLElement *nameElement = [TBXMLchildElementNamed:@"anyType"parentElement:getUserCheckResult];
while (nameElement) {
NSString *name = [TBXMLtextForElement:nameElement];
NSLog(@"name = %@ ",name);
[soapResultsaddObject:name];
nameElement = [TBXMLnextSiblingNamed:@"anyType"searchFromElement:nameElement];
}
if ([matchingElementisEqualToString: @"getUserCheck"]) {
[self.delegatekankan:soapResults];
}elseif ([matchingElementisEqualToString:@"GetWellList_Super_AddWellType"])
{
[self.delegatekankan1:soapResults];
}elseif ([matchingElementisEqualToString: @"GetTemplateItems"])
{
[self.delegatekankan2:soapResults];
}
else if ([matchingElementisEqualToString: @"checkMD5WithTemplate"])
{
[self.delegatekankan3:soapResults];
}
}
}
}
}
}
//下面是sax解析 现在弃用
// 开始解析一个元素名
//-(void) parser:(NSXMLParser *) parser didStartElement:(NSString *) elementName namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *) qName attributes:(NSDictionary *) attributeDict {
// str = [NSString stringWithFormat:@"%@Response",matchingElement];
// if ([elementName isEqualToString:str]) {
// elementFound = YES;
// }
//
//}
//
//// 追加找到的元素值,一个元素值可能要分几次追加
//-(void)parser:(NSXMLParser *) parser foundCharacters:(NSString *)string {
// if (str) {
// [soapResults addObject:string];
//// 结束解析这个元素名
//-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
// if ([matchingElement isEqualToString: @"getUserCheck"]) {
// [self.delegate kankan:soapResults];
// }else if ([matchingElement isEqualToString: @"GetWellList_Super_AddWellType"])
// {
// [self.delegate kankan1:soapResults];
// }
//// 解析整个文件结束后
//- (void)parserDidEndDocument:(NSXMLParser *)parser {
// if (soapResults) {
// soapResults = nil;
//// 出错时,例如强制结束解析
//- (void) parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
//}
@end