All,
I have XML in the following format:
Some observations:
An infinite number of WHAT inside of the WHAT?
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.
Consequently, in didStartElement:
, check if the element name is @"Description"
and if so, create a new NSMutableDictionary
instance that you store in an ivar.
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.
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.