1

I need to connect to my SOAP web service from IPhone, I tried the approach below and it worked:

NSURL *url = [NSURL URLWithString:@"http://w3schools.com/webservices/tempconvert.asmx"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url] 
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];
[theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue: @"http://tempuri.org/CelsiusToFahrenheit" forHTTPHeaderField:@"SOAPAction"];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

if( theConnection )
{
webData = [[NSMutableData data] retain];
}
else
{
 NSLog(@"theConnection is NULL");
}

this way I cen get response from Service, First I need to create some classes, then I need to parse this XML and fill my data objects with it. My questions is:

What is wrong with this method? Becase there are libraries like WSDL2ObjC which creates code from my wsdl, or cSOAP which is a client/server SOAP library.

Is the only point of using a SOAP library is that I dont need to write the classes that my XML will be parsed into? with my aprroach above it only seems like after parsing the xml everything will be ok, or I am missing something?

For why dont use WSDL2Objc, please see my question about it here There is no documentation!

Community
  • 1
  • 1
Spring
  • 11,333
  • 29
  • 116
  • 185

1 Answers1

2

If you get the correct response from the web service your job is done. (Your code looks ok.)

Regarding the XML parsing of the result - you can make your implementation. For a simple result you can even use NSScanner and not go deep into the NSXMLParser. And if you are interested in parsing the XML in the response I suggest using the native XML parsing delegation mechanism of NSXMLParser because Apple believes that this is the correct approach in handling XML as I recall. (If there is a JSON part in the response you can handle that too with the JSON library for iOS.)

Probably, the libraries are done to be more universal and to provide you some performance benefits. But I guess that your own implementation will give you the best performance benefits. However, before using some libraries you could have a look at the code or do some measurements. I was looking for a library at a time because I did not want to implement a feature, but when I saw how someone was creating a string at one place:

for()
{
    newSize = oldSize++;
    newStr = malloc(newSize);
    newStr = strcat(oldStr, newCharStr);
    free(oldStr);
}

I was a bit astonished, because he/she could have made only 1-2 malloc calls in the situation and a lot less copying.

Well, just keep efficiency in mind and you'll be fine.

Community
  • 1
  • 1
Teodor Kostov
  • 313
  • 1
  • 11
  • I am new in obkective-c so do not know well how to write efficent codes..I use gxmldata for parsing, should I parse my xml into objects and then re-create my xml from my objects when 'i am done or is there a dom document kind of thing that I could make my all edit in the xml document and natively convert it into a xml ready to be sent – Spring Jun 25 '11 at 10:25
  • 1
    Apple's idea behind the delegation method of parsing the XML is not to create the DOM of the XML. So rather than the usual approach: parse the XML > create the DOM > access the specific object you are looking for - you have the delegation method: parse the XML > access the specific element you are looking for. This way you do not need the whole structure of the XML in memory. You will get the specific information you are looking for or create the data model that you want. – Teodor Kostov Jun 25 '11 at 11:51
  • can I also edit XML that way? and how efficient is that using the NSXMLPArser – Spring Jun 25 '11 at 13:55
  • The parser just reads info and recognizes different logical structures. To create the XML structure you will need to write you own routines that transform your iOS objects into XML OR check out the [Property List Programming Guide](http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/PropertyLists/Introduction/Introduction.html) (plist is actually XML) OR use a framework like [How To Read and Write XML Documents with GDataXML](http://www.raywenderlich.com/725/how-to-read-and-write-xml-documents-with-gdataxml) for example (this is the first result that google showed). – Teodor Kostov Jun 26 '11 at 09:20
  • I would start with the plist guide and I believe that this would be sufficient. If you, however, need to store some serious amounts of data in the future you should also go through [Core Data Programming Guide](http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreData/cdProgrammingGuide.html). – Teodor Kostov Jun 26 '11 at 09:24