Parse xml data and assign element values dynamically to object
Im stuck with this:
I need to fetch xmldata from a website, parse it and bring it into a more accessible form. I've read a few tutorials/guides about how to parse xml and they more or less all have the same approach: Creating an instance of NSXMLParser and the delegates assign the parsed values to an object.
Though this is basically working for me, all the sample code I saw seemed not very generic to me. There is always just one target object and there are a lot of if/else statements matching the xml elements with hard coded strings when assigning the values to the object properties. Since I need to get data from different xml files and store each in a different object this would be quite hard to maintain.
Luckily the name of the xml elements match the object properties so I can dynamically check whether the corresponding property exists and assign it if so. But the different objects are driving me crazy. I tried several things but could not get anything to work. Does I really have to implement all the possible classes and create an instance for the suiting one (according to a passed class name or something like that)? My inital idea was to pass the object to the xmlparser and let the parser assign the values. But as I said I couldn't get this working.
If someone has an idea how to make this work (or maybe a complete different approach) I would really appreciate it if he could describe it here or give me some hint into the right direction.
Thanks!
Last edited by tribeca; 12-06-2010 at 06:40 AM.
|