2012-10-18 117 views

回答

1

首先,添加委托的URLConnection并在你的头文件pasrsing:

代表补充说明的是:

  1. NSURLConnectionDelegate
  2. NSXMLParserDelegate

,那么你必须请拨打XML网址:

NSURL *url = [NSURL URLWithString:URLString]; 
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url]; 
    NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]]; 
    [theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; 
    [theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"]; 
    [theRequest setHTTPMethod:@"POST"]; 
    [theRequest setTimeoutInterval:10]; 
    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; 
    if(theConnection) 
    { 
     responseData = [[NSMutableData data] retain]; 
    } 

实现NSURLConnectionDelegate方法,这将是这样的:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 
    NSLog(@"didReceiveResponse"); 
    [responseData setLength:0]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 
    NSLog(@"didReceiveData"); 
    [responseData appendData:data]; 
} 

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { 
    NSLog(@"didFailWithError = %@",[error description]); 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection { 
    NSLog(@"connectionDidFinishLoading"); 

    xmlParser= [[NSXMLParser alloc]initWithData:responseData]; 
    [xmlParser setDelegate:self]; 
    [xmlParser setShouldResolveExternalEntities: YES]; 
    [xmlParser parse]; 

    [responseData release]; 
    [connection release]; 
} 

然后添加XMLPARSER委托方法:

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict { 
//The tags in the xml data, the opening tags 
} 

-(void)parser:(NSXMLParser*)parser foundCharacters:(NSString*)string { 
//The data in tags 
} 

-(void)parser:(NSXMLParser*)parser didEndElement:(NSString*)elementName namespaceURI:(NSString*)namespaceURI qualifiedName:(NSString*)qName { 
//The end of each tags 
} 

下面是用于处理XML数据的一个很好的教程:Tutorial

+0

am gettng响应问题是当我用xml解析器解析时代表我遇到问题 – Sowji

+0

解析器didStartElement currentElement = [elementName copy];如果([elementName isEqualToString:@“MainMenu”]) \t { sub = [[NSMutableString alloc] init]; subsub = [[NSMutableString alloc] init]; sub1 = [[NSMutableString alloc] init]; } – Sowji

+0

你在这里有什么问题,一切看起来都正确。 – Scar