Parsing xml: how to check the version number

Discussion in 'iOS Programming' started by doanelinde, Aug 27, 2009.

  1. doanelinde macrumors member

    Joined:
    Jul 24, 2009
    #1
    I'm parsing an xml file into a coredata database and am just wondering how I would go about checking for updated versions of the xml file using the version number? Is there an easy way to do this?


    The database would only be updated every couple of months or so, and there would be massive overhead parsing the whole file every time the app runs..
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    Do you have control of the content of the XML file? If so, maybe add an attribute or node with the version number and then just parse for that. NSXMLParser doesn't require the whole file to be parsed; it does it piecemeal.
     
  3. doanelinde thread starter macrumors member

    Joined:
    Jul 24, 2009
    #3
    Yes I have access to the file. Is there not some generic way to access the version number though, I thought there was?
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    XML files can have a version number in their header but that is in reference to the version of XML the file is using and not the version of the content. I'd suggest adding a <contentVersion> node to the file and parsing for that.
     
  5. doanelinde thread starter macrumors member

    Joined:
    Jul 24, 2009
    #5
    Ok, say I'm using the books example for example, instead of the first book object I just have content and then when I'm parsing I have an if statement checking whether elementname=book or content?
     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    Which books example?
     
  7. doanelinde thread starter macrumors member

    Joined:
    Jul 24, 2009
    #7
    This is a simple example with <book> elements.

    This is the tutorial, or click here to see the XML file.
     
  8. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    Hmm. Can you include a sample of your XML file?
     
  9. doanelinde thread starter macrumors member

    Joined:
    Jul 24, 2009
    #9
    Yep, this is what the xml file looks like:

    <?xml version="1.0" encoding="UTF-8"?>
    <Books>
    <Book id="1">
    <title>Circumference</title>
    <author>Nicholas Nicastro</author>
    <summary>Eratosthenes and the Ancient Quest to Measure the Globe.</summary>
    </Book>
    <Book id="2">
    <title>Copernicus Secret</title>
    <author>Jack Repcheck</author>
    <summary>How the scientific revolution began</summary>
    </Book>
    <Book id="3">
    <title>Angels and Demons</title>
    <author>Dan Brown</author>
    <summary>Robert Langdon is summoned to a Swiss research facility to analyze a cryptic symbol seared into the chest of a murdered physicist.</summary>
    </Book>
    <Book id="4">
    <title>Keep the Aspidistra Flying</title>
    <author>George Orwell</author>
    <summary>A poignant and ultimately hopeful look at class and society, Keep the Aspidistra Flying pays tribute to the stubborn virtues of ordinary people who keep the aspidistra flying.</summary>
    </Book>
    </Books>
     
  10. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #10
    Okay, I would just add a <contentVersion> node ahead of the <Books> node and then you can also parse for elementName == "contentVersion".
     
  11. doanelinde thread starter macrumors member

    Joined:
    Jul 24, 2009
    #11
    Ok perfect, sounds simple enough, thanks very much!
     

Share This Page