Parsing the Xml data in xcode - objective-c

I am beginner to iphone. My requirement is to invoke the sap webservice in iphone.I got the result data which is in xml form.The result is to be in NSString which is of Xml form.Then how to get that result into the array in xcode.Please help me.

Here is a great library on GitHub-XML to NSDictionary
It isn't quite an NSArray but xml files are rarely just Arrays, so this provides an intermediate, dictionaries when needed, arrays if possible.

1. You can use NSXMLParser for this purpose, read through the delegate methods in https://developer.apple.com/library/ios/documentation/cocoa/reference/NSXMLParserDelegate_Protocol/Reference/Reference.html
2. This question is pretty repetitive, you could have just googled "Parsing the Xml data in iPhone" or "Parsing the Xml data using xCode" you would have got loads of results. Go trhough this ->
http://www.xcode-tutorials.com/parsing-xml-files/
and maybe this-> http://www.edumobile.org/iphone/iphone-programming-tutorials/parsing-an-xml-file/
3. Next time please do search.

Your question is quite general and it's difficult to give you a specific answer.
Here some guidelines to consume web services in iOS.
First, you need to have a mechanism that helps you to consume the ws. To do it, you can use NSUrlConnection class (see NSURLConnection Class) or use a the ASIHttpRequest framework (see ASIHTTPRequest). In this manner you can make requests and dowload response messages.
Since you use soap messages, you have first to create the request message manually. You can use class method of NSString stringWithFormat or use ASIFormDataRequest class of ASIHttpRequest framework.
Finally, if you receive a message that is like the following you posted in your comment, you need to parse it. Remember that is a soap message and it doesn't have only your tags. To do it, you can use NSXMLParser class (see NSXMLParser Class) or use GDataXML parser (see how-to-read-and-write-xml-documents-with-gdataxml).
Out there there are plenty of tutorials or posts on how to "consume web services on iOS". You can find them. In addition, there are also some kits that, taken the your service, create class wrappers to consume your services. In this case you don't need to create or parse manually masseges.
A last remark. When you need to consume data taken from a service, maybe a REST architecture could be easier to set up.
Hope it helps.

Related

convert an object to JSON in objective c

I work in tool theos projects and i want convert an object into JSON.
I need a easy to use library with examples for converting NSObjects to JSON and back again
I check a lot of question like this but i can't use them.
I use JSONModel library but i have a lot of errors.
Anybody body have a good tutorial or a sample code to convert NSObject to JSON?
I don't have any idea whether I can created a json or not.
How can I fix this?
Look at my library - https://github.com/DimasSup/BaseMangedObjectModel
With it you can serialize/deserialize any your class. aslo can save it to SQLite database if needed. Also there are NetworkHelper class which help you send/receive your classes from remote server.

Consuming ASP.NET(Asmx) web service (Cocoa/Objective-C)

I have ASP.NET webservice(Asmx) which I need to consume in Cocoa/Objective-C. What is the simplest/optimal way to achieve this? I have done the implementation where we have many delegates for XMLParser and connection. like didStartElement, didEndElement....
Is there any other way to achieve this? In .NET I have done a similar implementation where I have a proxy class and when I make a call to the web method I get a response which is the return value of the web method rather than parsing the response xml.
Please let me know.
Try out some of the existing network frameworks before writing your own (RESTKit, AFNetworking, etc.) - They all have good tutorials on how to easily consume webservices.
A little bit off-topic, but it seems that your Webservice returns a XML response. I would (but that's just me!) try to change that to JSON. Cocoa/Obj-C provides built-in functions to deserialize JSONs, and the aforementioned frameworks also work quite well with this format. It's far easier to use than NSXMLParser etc.

Objective-C Sudzc WSDL Generator Alternative

Sudzc (www.sudcz.com) appears that it is no longer a project that is being contributed to!
After a year of no commits and some serious bugs especially with returning list of objects this helpful tool appears to be fading into the black holes of the internet.
I was wondering if anyone knew of an alternative tool that works the same way as this?
Or are soap requests no longer a preferred method of data transfer? Is json a better approach using rest clients?
Bonus question: Is there a way to make sudzc handle a returned list of objects
Some alternatives are:
Easy WSDL
Wsdl2Code
This answer mentions another alternative, wsdl2objc.
Within the generated SoapRequest.m file there you can find the implementation of
- (void) connectionDidFinishLoading:(NSURLConnection *) connection
Look for
if([deserializeTo respondsToSelector: #selector(initWithNode:)])
{
element = [element childAtIndex:0];
output = [deserializeTo initWithNode: element];
}
and remove the line with "element = [...]". If you do so keep in mind that you could have to do fixes in other parts of your generated .m files because of this change. This depends heavily on your WebService layout. Hope this helps.

Creating an Objective-C API

I have never made an API in objective-c, and need to do this now.
The "idea" is that I build an API which can be implemented into other applications. Much like Flurry, only for other purposes.
When starting the API, an username, password and mode should be entered. The mode should either be LIVE or BETA (I guess this should be an NSString(?)), then afterwards is should be fine with [MyAPI doSomething:withThisObject]; ect.
So to start it [MyAPI username:#"Username" password:#"Password" mode:#"BETA"];
Can anyone help me out with some tutorials and pointer on how to learn this best?
It sounds like what you want to do is build a static library. This is a compiled .a file containing object code that you'll distribute to a client along with a header file containing the interface. This post is a little outdated but has some good starting points. Or, if you don't mind giving away your source code, you could just deliver a collection of source files to your client.
In terms of developing the API itself, it should be very similar to the way you'd design interfaces and implementations of Objective-C objects in your own apps. You'll have a MyAPI class with functions for initialization, destruction, and all the functionality you want. You could also have multiple classes with different functionality if the interface is complex. Because you've capitalized MyAPI in your code snippet, it looks like you want to use it by calling the class rather than an instance of the class - which is a great strategy if you think you'll only ever need one instance. To accomplish this you can use the singleton pattern.
Because you've used a username and password, I imagine your API will interface with the web internally. I've found parsing JSON to be very straightforward in Objective-C - it's easy to send requests and get information from a server.
Personally I would use an enum of unsigned ints rather than a NSString just because it simplifies comparisons and such. So you could do something like:
enum {
MYAPI_MODE_BETA,
MYAPI_MODE_LIVE,
NUM_MYAPI_MODES
};
And then call:
[MyAPI username:#"Username" password:#"Password" mode:MYAPI_MODE_BETA];
Also makes it easy to check if they've supplied a valid mode. (Must be less than NUM_MYAPI_MODES.)
Good luck!

Objective-C: NSInputStream and NSOutputStream for testing purposes

I have an class in my application that is initialized with an NSInputStream. For testing purposes i want to write data on an NSOutputStream which then is received by that input stream.
This should trigger the NSStreamEventHasBytesAvailable event.
The only thing is that i do not know how to set this up. Does anybody has an idea? Or suggestions how to unit test a class with has an NSStream as dependency.
Thanks
This is actually not very tricky, I've described how to do it using the SimpleURLConnection sample code available on the apple developer site.
This is the question and my answer there will work here too.
This is actually really tricky since it appears that you can only create an input stream using a file, a URL or an NSData. You can't connect an input stream to an output stream, although you might be able to write a single class that implements both interfaces.
The easiest way to do the unit test would probably be to create your input stream from an NSData whose bytes you have already read in from the output stream.