Re: XML to Plist
Re: XML to Plist
- Subject: Re: XML to Plist
- From: Sandro Noël <email@hidden>
- Date: Tue, 28 Dec 2010 13:34:44 -0500
Greg, thank you.
the glitch that breaks all my current logic is when there are elements with the same name at the same level.
for instance in a RSS,
rss
chanel
item
item
item
this breaks the logic I can apply to a NSDictionary, because the previous item gets overwritten with the current one.
That is why I've attempted to put them in array's but that had adverse effects also.
This procedure produces the correct results if there is no repeating elements at the same level,
- (NSDictionary *) plist{
NSMutableDictionary *resultDict = [[[NSMutableDictionary alloc] init] autorelease];
if ([self hasChildren]){
NSMutableDictionary *child = [[[NSMutableDictionary alloc]init] autorelease];
for (OSXMLElement *element in _children){
[child addEntriesFromDictionary:[element plist]]; }
[resultDict setValue:child forKey:_elementName];
}
else {
[resultDict setValue:_elementText forKey:_elementName];
}
return resultDict;
}
I've asked the list because I seem to be in an impasse here with this particular logic problem.
I'm sure i'm not the only one who had that unfortunate mind block. :)
best regards.
Sandro.
On 2010-12-28, at 1:10 PM, Greg Guerin wrote:
> Sandro Noël wrote:
>
>> - (NSDictionary *) plist{
>> NSMutableDictionary *resultDict = [[[NSMutableDictionary alloc] init] autorelease];
>> if ([self hasChildren]){
>> NSMutableArray *child = [[[NSMutableArray alloc]init] autorelease];
>> for (OSXMLElement *element in _children){
>> // if thiselement has children add them to an array
>> [child addObject:[element plist]];
>> }
>> [resultDict setValue:child forKey:_elementName];
>> }
>> // just a regular node.
>> else {
>> [resultDict setValue:_elementText forKey:_elementName];
>> }
>> return resultDict;
>> }
>
> If you don't want nodes stored in an array, then don't use NSMutableArray.
>
> I think you need to step back from the coding and do a better analysis. At each step of the logical analysis, given a type of XML node as input, write down exactly what actions should occur for the desired output. Don't write code, just write down brief action descriptions.
>
> For example, I see no arrays in your desired output, so there shouldn't be any need for creating an array. If you don't create an array for children, analyze what is needed instead.
>
> You could also benefit by doing an analysis (i.e. write action descriptions) of the code you have now. When you get to the part that says "Create array. Fill it with every child", think about what that means.
>
> FWIW, this isn't a Cocoa problem, it's a logic problem. Get the logic right first, then the Cocoa code should be plain.
>
> -- GG
>
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden