Parse Nested XML Objective C - NSXMLParser

前端 未结 1 1111
陌清茗
陌清茗 2021-01-07 15:18

All,

I have XML in the following format:


    
        
        
         


        
相关标签:
1条回答
  • 2021-01-07 15:59

    Some observations:

    1. An infinite number of WHAT inside of the WHAT?

    2. Assuming there can be more than one Description element, the outer data structure in which you store the contents must be a NSMutableArray, not a dictionary. You then use one mutable dictionary per Description element.

    3. Consequently, in didStartElement:, check if the element name is @"Description" and if so, create a new NSMutableDictionary instance that you store in an ivar.

    4. In foundCharacters:, you always have to append the new characters to the existing currentElementValue because the method can be called multiple times for each element's contents. I see many people do this wrong despite the fact that Apple's sample code clearly demonstrates the correct way.

    5. In didEndElement:, do this:

      • If the element name is @"desc" or @"IP", assign currentElementValue to the corresponding key in your current mutable dictionary. Don't forget to release currentElementValue before you set it to nil. You currently have a memory leak in your code because you're not doing that.

      • If the element name is @"Description", add the current mutable dictionary to the mutable array. Release the dictionary and set the ivar to nil. A new dictionary will be created the next time you encounter a @"Description" element in didStartElement:.

      • If the element name is @"linked-list", the mutable array will contain all the dictionaries and you're done.

    0 讨论(0)
提交回复
热议问题