How do you parse XML that have attributes and elements

Discussion in 'iOS Programming' started by webznz, Oct 12, 2011.

  1. webznz macrumors member

    Joined:
    Mar 8, 2011
    Location:
    Hobbitin
    #1
    Hey guys, I have been trying to parse some xml that have attributes and elements.. I have parsed xml with only element but not xml with both..

    The XML I am trying to parse looks like this

    Code:
    <Rows>
        <Row SKATERID="706" MANUFACTURER="GAZ" ISFACT="F" ISSKATE="F"/>
        <Row SKATERID="318" MANUFACTURER="MAN" ISFACT="F" ISSKATE="T"/>
        //...
     </Rows>

    I know that I need to use NSXMLPArser delegate didStartElement and didEndElement... I am hoping you guys can show me some straight forward code examples to get me on my way.
     
  2. Sykte macrumors regular

    Joined:
    Aug 26, 2010
    #2
    The delegate call back didStartElement passes a dictionary of attributes.
     
  3. RonC macrumors regular

    Joined:
    Oct 18, 2007
    Location:
    Chicago-area
    #3
    You need to implement didStartElement and maybe didEndElement. I did all of the API in a utility class (RCXMLDictionary) that I use as a base class for a bunch of more specific XML format - turned out that solving the XML parser interface once was easier for me than trying to copy and solve it several times for each of those formats.

    I'm working on posting it to github or sourceforge; when I do, I'll follow-up with a link.

    Just a note: NSXMLParser seems to have a leak; it doesn't seem to be huge, but it does piss me off that my app leaks because the library leaks. All my leaks (other than NSXMLParser-generated ones) were found and eradicated.
     
  4. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #4
    I've given up on writing individual parsers for xml. Instead there are a handful of available generic xml parsers that will (or should) parse any xml into Foundation objects. Look at for example

    http://nfarina.com/post/2843708636/a-lightweight-xml-parser-for-ios

    and there are others. This makes XML parsing more like JSON parsing.
     

Share This Page