Parsing XML

Discussion in 'iOS Programming' started by Switz213, Jul 3, 2009.

  1. Switz213 macrumors 6502

    Joined:
    May 26, 2008
    #1
    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.
     
  2. cromwell64 macrumors regular

    Joined:
    Jun 30, 2008
    #2
    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.
     
  3. Saladinos macrumors 68000

    Saladinos

    Joined:
    Feb 26, 2008
    #3
    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.
     
  4. Switz213 thread starter macrumors 6502

    Joined:
    May 26, 2008
    #4
    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
     
  5. Saladinos macrumors 68000

    Saladinos

    Joined:
    Feb 26, 2008
    #5
    Can't help you with the server-side stuff, but JSON is a great way of transferring structured data.
     
  6. jnic macrumors 6502a

    Joined:
    Oct 24, 2008
    Location:
    Cambridge
  7. Switz213 thread starter macrumors 6502

    Joined:
    May 26, 2008
    #7
    Whatever is the easiest. I assume PHP would be best.
     
  8. Switz213 thread starter macrumors 6502

    Joined:
    May 26, 2008
    #8
    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?
     
  9. drivefast macrumors regular

    Joined:
    Mar 13, 2008
    #9
    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, and tons of sample code. you can do it.
     
  10. Saladinos macrumors 68000

    Saladinos

    Joined:
    Feb 26, 2008
    #10
    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.
     
  11. Switz213 thread starter macrumors 6502

    Joined:
    May 26, 2008
    #11
    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
     
  12. drivefast macrumors regular

    Joined:
    Mar 13, 2008
    #12
    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.
     
  13. Switz213 thread starter macrumors 6502

    Joined:
    May 26, 2008
    #13
    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.
     
  14. Saladinos macrumors 68000

    Saladinos

    Joined:
    Feb 26, 2008
    #14
    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.
     
  15. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #15
    As shown by the XMLPerformance sample app, NSXMLParser is not the only way to parse XML on the iPhone. As Yoda once said, "There is another..."
     

Share This Page