View Full Version : Parsing XML
Switz213
Jul 4, 2009, 01:31 AM
Okay, so I want to take an XML file and parse it. Would it be best to parse it through PHP on a server, then propagate it to the iPhone, or just parse it on the iPhone itself? What's easier? I'm also going to want to implement push, so I assume that the server would be the best way to go.
Any advice?
I see that parsing XML on the iPhone isn't exactly all to easy.
cromwell64
Jul 4, 2009, 06:54 PM
I don't know exactly what you are doing with the XML, but for the most part I would say that you would want to parse it on the server first. This is such a common task that there are plenty of server side scripts available that do this already. It seems like this would make it easier on you, and also easier on whoever is using your application since their phone won't have to spend the time and energy parsing the XML.
Saladinos
Jul 4, 2009, 07:00 PM
Yup. If you want push, do it on the server.
With push notifications, your server tells Apple's server what to push and the OS displays the message. Your app isn't involved in the showing of messages.
Besides, this method is better protected against updates and moves more code from the device. You can debug and fix server code if there's a bug without pushing updates through the lengthy approval process.
Switz213
Jul 6, 2009, 12:34 AM
Can anyone "push" me in the right direction for server side XML parsing? (no pun intended). How would I go about pulling this information off to the iPhone too?
Thanks
Saladinos
Jul 6, 2009, 07:53 AM
Can anyone "push" me in the right direction for server side XML parsing? (no pun intended). How would I go about pulling this information off to the iPhone too?
Thanks
Can't help you with the server-side stuff, but JSON is a great way of transferring structured data.
jnic
Jul 6, 2009, 08:12 AM
What language(s) are you using server-side?
Switz213
Jul 6, 2009, 09:56 AM
What language(s) are you using server-side?
Whatever is the easiest. I assume PHP would be best.
Switz213
Jul 6, 2009, 11:30 AM
Okay. I think I found an XML-->JSON converter for the server side. How do I take a remote XML and make a string out of it, though?
drivefast
Jul 6, 2009, 02:55 PM
what in the world are you guys talking about??
Switz, i may not understand properly what you want to do, but if you have a local (i.e. on your device) XML file that has to be parsed, then by all means you are much better off parsing it with the sdk's parser. when you parse XML contents, you transform text structured data into binary structured data; if you do the parsing server-side, you will have to transfer and receive data in a binary format, which is way more complicated programming-wise than XML parsing with the sdk class. there is avery good explanation of how xml parsing works here (http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/XMLParsing/XMLParsing.html), and tons of sample code. you can do it.
Saladinos
Jul 6, 2009, 06:11 PM
what in the world are you guys talking about??
Switz, i may not understand properly what you want to do, but if you have a local (i.e. on your device) XML file that has to be parsed, then by all means you are much better off parsing it with the sdk's parser. when you parse XML contents, you transform text structured data into binary structured data; if you do the parsing server-side, you will have to transfer and receive data in a binary format, which is way more complicated programming-wise than XML parsing with the sdk class. there is avery good explanation of how xml parsing works here (http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/XMLParsing/XMLParsing.html), and tons of sample code. you can do it.
As I understand it, he has an XML data feed containing structured data that he needs in the iPhone app. The original question was whether to download the data as an XML file to the device and parse there, or parse on a server and transfer the structured data to the device in some other way. A system allowing support for push notifications with as few modifications as possible was preferred.
In this case, the latter option is better. Push notifications require that the data be sent to Apple in a formatted manner. That is, you can't push a lump of XML to the device and have the app parse it; you need to push the notification title and content strings to Apple in a certain way.
If the only thing your server has is XML content, you'll need to parse it on the server to get the push notification title and content. If the server knew about those fields as objects, adding push support is simple.
Switz213
Jul 6, 2009, 06:52 PM
As I understand it, he has an XML data feed containing structured data that he needs in the iPhone app. The original question was whether to download the data as an XML file to the device and parse there, or parse on a server and transfer the structured data to the device in some other way. A system allowing support for push notifications with as few modifications as possible was preferred.
In this case, the latter option is better. Push notifications require that the data be sent to Apple in a formatted manner. That is, you can't push a lump of XML to the device and have the app parse it; you need to push the notification title and content strings to Apple in a certain way.
If the only thing your server has is XML content, you'll need to parse it on the server to get the push notification title and content. If the server knew about those fields as objects, adding push support is simple.
Thanks. That's exactly what I meant :)
So, what would be the best method of doing this? Would periodically downloading the XML to my server and then converting it over to JSON be? Any advice?
Thanks
drivefast
Jul 6, 2009, 07:13 PM
Saladinos, please keep in mind that push notifications payloads are limited to 256 octets in length. push notifications are not intended to send data, they are meant to notify the user that data is avaliable. although correct, due to this strong size limitation your advice may not apply for what the OP intended to do.
Switz213
Jul 6, 2009, 08:29 PM
Saladinos, please keep in mind that push notifications payloads are limited to 256 octets in length. push notifications are not intended to send data, they are meant to notify the user that data is avaliable. although correct, due to this strong size limitation your advice may not apply for what the OP intended to do.
This is for a website/internet business a friend is starting up. It is a DoD site, much like Woot.com and those others. I want to create an app that pushed the new items. They will be under 256 octets.
Saladinos
Jul 6, 2009, 08:36 PM
Saladinos, please keep in mind that push notifications payloads are limited to 256 octets in length. push notifications are not intended to send data, they are meant to notify the user that data is avaliable. although correct, due to this strong size limitation your advice may not apply for what the OP intended to do.
True. I think the OP needs to read Apple's push notification documents. This is going to turn in to a server implementation problem, and that's really not my area.
From my crude understanding, JSON is great because it allows you to send formatted data structures. XML also allows this, but I'm not a fan of NSXMLParser because of its stupid callback parsing methods.
If the server created the XML, it should have some internal object representation of the data. That stuff can be used to create a push message. However, if you have this internal object representation, you have a choice between sending information to the device as XML or JSON or some other format. Since parsing XML on the iPhone is such a pain, I'd recommend JSON.
The thing is, you're talking about where to parse the XML, which implies that your server isn't the creator of the XML data. If this is the case, I'd parse it on the server so you get that internal object representation of the data, then, as above, use whatever you like to communicate that data to the device and as push messages. If your server is the creator of the XML, you already have the internal object representation, and the server wouldn't have to parse anything anyway.
dejo
Jul 6, 2009, 09:57 PM
...but I'm not a fan of NSXMLParser because of its stupid callback parsing methods.
...
Since parsing XML on the iPhone is such a pain, I'd recommend JSON.
As shown by the XMLPerformance sample app (http://developer.apple.com/iphone/library/samplecode/XMLPerformance/index.html), NSXMLParser is not the only way to parse XML on the iPhone. As Yoda once said, "There is another..."
vBulletin® v3.8.6, Copyright ©2000-2012, Jelsoft Enterprises Ltd.