nicklockwood / XMLDictionary

[DEPRECATED]
http://charcoaldesign.co.uk/source/cocoa#xml-dictionary
Other
1.14k stars 235 forks source link

Odd behaviour in processing certain nodes. #36

Open jvanlint opened 9 years ago

jvanlint commented 9 years ago

Hi Nick,

I have the following XML

<?xml version="1.0" encoding="UTF-16" standalone="no"?>
<startXML>
    <Contact/>
    <Dashboards>
        <Name>Dagsomsætning ugedag</Name>
        <KPI>
            <KPIName>Total salg (tDKK)</KPIName>
            <CurrentValue>8281</CurrentValue>
            <TargetValue>592</TargetValue>
            <MaxTolerance>0</MaxTolerance>
            <MinTolerance>0</MinTolerance>
            <LowIsBetter>n</LowIsBetter>
            <LastUpdate>2015-04-18 19:20:23</LastUpdate>
            <Comment/>
            <URL/>
            <DataPoints>6846</DataPoints>
            <DataPoints>6255</DataPoints>
            <DataPoints>6268</DataPoints>
            <DataPoints>6662</DataPoints>
            <ShowElipses>y</ShowElipses>
        </KPI>
        <KPI>
            <KPIName>Total salg2 (tDKK)</KPIName>
            <CurrentValue>8281</CurrentValue>
            <TargetValue>592</TargetValue>
            <MaxTolerance>0</MaxTolerance>
            <MinTolerance>0</MinTolerance>
            <LowIsBetter>n</LowIsBetter>
            <LastUpdate>2015-03-18 09:20:23</LastUpdate>
            <Comment/>
            <URL/>
            <DataPoints>6846</DataPoints>
            <DataPoints>6255</DataPoints>
            <DataPoints>6268</DataPoints>
            <DataPoints>6662</DataPoints>
            <ShowElipses>y</ShowElipses>
        </KPI>
    </Dashboards>
    <Dashboards>
        <Name>Test Board</Name>
        <KPI>
            <KPIName>Test sales (tDKK)</KPIName>
            <CurrentValue>8281</CurrentValue>
            <TargetValue>592</TargetValue>
            <MaxTolerance>0</MaxTolerance>
            <MinTolerance>0</MinTolerance>
            <LowIsBetter>n</LowIsBetter>
            <LastUpdate>2015-03-18 09:20:23</LastUpdate>
            <Comment/>
            <URL/>
            <DataPoints>6846</DataPoints>
            <DataPoints>6255</DataPoints>
            <DataPoints>6268</DataPoints>
            <DataPoints>6662</DataPoints>
            <ShowElipses>y</ShowElipses>
        </KPI>
    </Dashboards>
</startXML>

I use the [NSDictionary valueForKeyPath] method. The first Dashboard seems to process ok with KPI being a dictionary of 9 key/pair values.

The last KPI does not however and just appears to be an array of 9 key/pair values.

Here is an image illustrating what I mean.

dfsxmlparser_m

Any ideas? Is it a bug or something I am not understanding? Why is it treating the 2nd dashboard KPI differently just because it is alone?

stkhapugin commented 9 years ago

@jvanlint Hi! It seems like in the first Dashboard tag you have three elements: 1 Name and 2 KPI's; while in the second you have 1 KPI and 1 Name. The second Dashboard parses to a @{@Name:@"Test Board", @"KPI":@{...}} dictionary, which sounds fairly reasonable The first Dashboard, however, has two KPI objects, so they are parsed into an array of dictionaries that you can reference by KPI key. That makes perfect sense as well, since you can't have two distinct objects stored by the same key.

If you want to use consistent keypaths, you should try setting alwaysUseArrays to YES so that the second dashboard dictionary will have an array of a single KPI instead of a dictionary under @"KPI" key.

Hope this helps