0
아래 코드는 주기율표에 요소의 기호 이름을 부여하는 webservice 프로젝트의 예입니다. 이 프로젝트를 실행할 때 xml 파서에서 문자열 값을 가져 오지 않았습니다.xml 파서로 파싱 한 후 xml 형식의 문자열 값을 얻는 방법
-(IBAction)action:(id)sender
{
NSString *soapFormat = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
"<soap:Body>\n"
"<GetElementSymbol xmlns=\"http://www.webserviceX.NET/\">\n"
"<ElementName>%@</ElementName>\n"
"</GetElementSymbol>\n"
"</soap:Body>\n"
"</soap:Envelope>\n",txtcelsius.text];
NSLog(@"connextion:%@",soapFormat);
NSURL *locationofWebservice=[NSURL URLWithString:@"http://www.webservicex.net/webservices/periodictable.asmx"];
NSMutableURLRequest *theRequest=[[NSMutableURLRequest alloc]initWithURL:locationofWebservice];
NSLog(@"sopa len=%d",[soapFormat length]);
NSString *msgLength=[NSString stringWithFormat:@"%d",[soapFormat length]];
[theRequest addValue:@"text/xml" forHTTPHeaderField:@"Content-type"];
[theRequest addValue:@"http://www.webserviceX.NET/GetElementSymbol" forHTTPHeaderField:@"SOAPAction"];
[theRequest addValue:msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
//the below encoding is used to send data over the net
[theRequest setHTTPBody:[soapFormat dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *connection=[[NSURLConnection alloc]initWithRequest:theRequest delegate:self];
if (connection)
{
webData=[[NSMutableData alloc]init];
}
else
{
NSLog(@"No connection");
}
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[webData setLength:0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[webData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Webservice" message:[error localizedFailureReason] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"DONE. Received Bytes: %d", [webData length]);
xmlParser= [[NSXMLParser alloc]initWithData:webData];
[xmlParser setDelegate:self];
[xmlParser parse];
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
NSLog(@"string in parse=%@",string);
nodeContent=[[NSMutableString alloc]init];
[nodeContent appendString:[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
NSLog(@"string in parse node=%@",nodeContent);
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if([elementName isEqualToString:@"GetElementSymbol"])
{
// finaldata=nodeContent;
NSLog(@"node content=%@",nodeContent);
txtft.text=nodeContent;
}
}
코드는주기적인 테이블 요소의 상징을 제공하는 웹 서비스 프로젝트의 한 예입니다. 호스트 측의 응답은 다음 형식으로되어 있습니다.
<string xmlns="http://www.webserviceX.NET">
<NewDataSet> <Table> <Symbol>H</Symbol> </Table> </NewDataSet>
</string>
어떻게 이것을 문자열로 변환하고 텍스트 필드로 표시 할 수 있습니까?