The below given code is an example of webservice project which giving the symbol name of elements in periodic table. When I ran this project I didn't get the string value from xml parser.
-(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;
}
}
The code is an example of webservice project which giving the symbol of periodic table element. The response from the host side is in the below format:
<string xmlns="http://www.webserviceX.NET">
<NewDataSet> <Table> <Symbol>H</Symbol> </Table> </NewDataSet>
</string>
How can I convert this into string and display it into a textfield?
If you want to parse XML data, look into XMLDictionary. This is an easier way to parse the information and even easier to get at it.
It is an easy framework that easily parses your data into a neat and ordered hierarchy of NSDictionaries and NSArrays and all the data will appear as NSStrings.