views:

165

answers:

2

I have a fair bit of experience working with the iPhone SDK. I have a couple fully developed apps including a utility application for tracking data and a board game. I would consider myself a novice on the iPhone SDK. I also have no issues with reviewing documentation and examples for hours until I understand something, as long as the documentation exists and makes sense.

I am not however an expert at XML documents and parsing attributes and creating documents. I have written in the past PHP code to access web services and complete transactions via SOAP XML messaging. I need to do a similar application using iPhone SDK. Here is what I need.

1) Create XML messages to send to a SOAP service 2) Receive XML messages from a SOAP service 3) Parse the XML response messages into a custom object

All of the URL and SOAP part is relatively straight forward. Where I start to get discouraged is the area of creating and parsing the XML messages. Using PHP these tasks are very simple as the language has built in SOAP clients and you can easily class map objects to SOAP messaging and the heavy lifting of the XML processing is just taken care of automatically. I have considered writing a proxy PHP service and simplifying what is needed on the iPhone side but this just complicates what I want and adds a dependency on another app to work in conjunction with the web service I'm accessing.

It seems that in the iPhone SDK or objective-c in general you have to be a pro at the construction and the parsing of XML messages on your own. I am really discouraged at the idea of creating tons of custom code to parse each message element by element. I've taken a small look at TouchXML / KissXML and I am not thinking they are what I need.

The real key objective here is that I want to easily receive an XML message and some how get the elements and their values copied over to an object. How can this be done without hours of writing parsing classes and understanding all of the guts of creating XML documents?

+1  A: 

I have used Matt Gallagher's XPathQuery to great success on the iPhone. It's simple and straight-forward.

I've found, though, that the messages I have need to parse are drastically simpler than what XML (and even his simplified parser) is capable of. I've modified his source to return a data structure that's much easier to work with -- instead of having keys a bunch of keys (nodeName, nodeContent, nodeAttributeArray, attributeName, ...) for each node, I simply create nested arrays and dictionaries with the node name as the key and the content as the object directly. I completely ignore attributes, and I only use arrays if there's more than one node with the same name. Here's that code. Note that it's only been tested on the XML that I care to work with. :)

Edit: whoops, I just realized you're also looking for XML creation. This just does parsing. I'll leave it here as a partial answer.

Matt B.
Thanks for the answer here. I'll check out XpathQuery and see how it does. Hopefully what I am looking for. I'll vote up your answer after I check it out. The writing xml is not so bad. If I had to I could build the strings by hand for the small queries I am doing. The trick is in getting a SOAP response and parsing the data I get back into some custom objects. I really don't want to fiddle with XML. I am surprised Apple doesn't give us more to work with web services in the SDK. I hate web services and avoid at all costs. But a device that is always connected should have this stuff available.
Hazmit
A: 

It seems like what you really want is this:

http://code.google.com/p/wsdl2objc/

As it will create objects for you from a WSDL, and provide support code to send/receive them.

However what is mostly happening, is that people are not using SOAP for mobile web services, but instead are going to RESTFUL approaches.

That includes posting form data in HTTP POST requests or querying via GET, then getting back either JSON or plists which turn into dictionaries (JSON is more widely understood by server developers BUT you have to be more careful to correctly define the structures being passed, since you can do things like have a dictionary with the same key repeated... plists are more robust in that you can't really build anything "wrong" but is less standard on the server side).

If you have to use SOAP though, WSDL2ObjC is probably going to save you a lot of mapping.

Kendall Helmstetter Gelner
I agree that it would be best to stay away from the SOAP stuff and stick more with the JSON and PLIST side of things. Unfortunately I don't have any control over the server side. I am integrating to a web-service I don't have control over.I have looked into the WSDL2ObjC and it looked promising but very daunting on how to actually use the classes that are generated. I'll give it another pass though.
Hazmit