1

我正在使用 SUDZC 连接到 Web 服务。原始代码不断覆盖响应的值。我将代码修改为:

// Deserializes the element in a dictionary.
+(id)deserializeAsDictionary:(CXMLNode*)element {

if([element childCount] == 1) {
    CXMLNode* child = [[element children] objectAtIndex:0];
    if([child kind] == CXMLTextKind)
    {
        // NSLog(@"returnED:%@",[[[element children] objectAtIndex:0] stringValue]);
        return [[[element children] objectAtIndex:0] stringValue];

    }
}

NSMutableDictionary* d = [NSMutableDictionary dictionary];
for(CXMLNode* child in [element children]) {
    id v = [Soap deserialize:child];
    if(v == nil) { v = [NSNull null]; }



    [d setObject:v forKey:[child name]];
    // code added from greg to fix dictionary over writting
    NSString* key = [child name];

    id check = [d objectForKey:key];
    if( check != nil ) {

        NSInteger next = 1;
        key = [NSString stringWithFormat:@"%@%d", [child name], next];
        check = [d objectForKey:key];
        while( check != nil ) {

            next++;
            key = [NSString stringWithFormat:@"%@%d", [child name], next];
            check = [d objectForKey:key];

        }
        [d setObject:v forKey:key];

    }
}

return d;
}

现在,我收到一堆空值以及我的预期值:

2012-08-02 08:25:22.848 Management[13416:707] TC Keys: {
GetCashiersResult =     {
    diffgram =         {
        NewDataSet =             {
            TC =                 {
                "CSHR_NUM" = 7;
                "CSHR_NUM1" = 7;
                "CSHR_POS_NAME" = Randy;
                "CSHR_POS_NAME1" = Randy;
            };
            TC1 =                 {
                "CSHR_NUM" = 999;
                "CSHR_NUM1" = 999;
                "CSHR_POS_NAME" = "Apple Tester";
                "CSHR_POS_NAME1" = "Apple Tester";
            };
            TC2 =                 {
                "CSHR_NUM" = 8;
                "CSHR_NUM1" = 8;
                "CSHR_POS_NAME" = Jonathan;
                "CSHR_POS_NAME1" = Jonathan;
            };
            TC3 =                 {
                "CSHR_NUM" = 54;
                "CSHR_NUM1" = 54;
                "CSHR_POS_NAME" = LEXIE;
                "CSHR_POS_NAME1" = LEXIE;
            };
            TC4 =                 {
                "CSHR_NUM" = 7;
                "CSHR_NUM1" = 7;
                "CSHR_POS_NAME" = Randy;
                "CSHR_POS_NAME1" = Randy;
            };
        };
        NewDataSet1 =             {
            TC =                 {
                "CSHR_NUM" = 7;
                "CSHR_NUM1" = 7;
                "CSHR_POS_NAME" = Randy;
                "CSHR_POS_NAME1" = Randy;
            };
            TC1 =                 {
                "CSHR_NUM" = 999;
                "CSHR_NUM1" = 999;
                "CSHR_POS_NAME" = "Apple Tester";
                "CSHR_POS_NAME1" = "Apple Tester";
            };
            TC2 =                 {
                "CSHR_NUM" = 8;
                "CSHR_NUM1" = 8;
                "CSHR_POS_NAME" = Jonathan;
                "CSHR_POS_NAME1" = Jonathan;
            };
            TC3 =                 {
                "CSHR_NUM" = 54;
                "CSHR_NUM1" = 54;
                "CSHR_POS_NAME" = LEXIE;
                "CSHR_POS_NAME1" = LEXIE;
            };
            TC4 =                 {
                "CSHR_NUM" = 7;
                "CSHR_NUM1" = 7;
                "CSHR_POS_NAME" = Randy;
                "CSHR_POS_NAME1" = Randy;
            };
        };
    };
    diffgram1 =         {
        NewDataSet =             {
            TC =                 {
                "CSHR_NUM" = 7;
                "CSHR_NUM1" = 7;
                "CSHR_POS_NAME" = Randy;
                "CSHR_POS_NAME1" = Randy;
            };
            TC1 =                 {
                "CSHR_NUM" = 999;
                "CSHR_NUM1" = 999;
                "CSHR_POS_NAME" = "Apple Tester";
                "CSHR_POS_NAME1" = "Apple Tester";
            };
            TC2 =                 {
                "CSHR_NUM" = 8;
                "CSHR_NUM1" = 8;
                "CSHR_POS_NAME" = Jonathan;
                "CSHR_POS_NAME1" = Jonathan;
            };
            TC3 =                 {
                "CSHR_NUM" = 54;
                "CSHR_NUM1" = 54;
                "CSHR_POS_NAME" = LEXIE;
                "CSHR_POS_NAME1" = LEXIE;
            };
            TC4 =                 {
                "CSHR_NUM" = 7;
                "CSHR_NUM1" = 7;
                "CSHR_POS_NAME" = Randy;
                "CSHR_POS_NAME1" = Randy;
            };
        };
        NewDataSet1 =             {
            TC =                 {
                "CSHR_NUM" = 7;
                "CSHR_NUM1" = 7;
                "CSHR_POS_NAME" = Randy;
                "CSHR_POS_NAME1" = Randy;
            };
            TC1 =                 {
                "CSHR_NUM" = 999;
                "CSHR_NUM1" = 999;
                "CSHR_POS_NAME" = "Apple Tester";
                "CSHR_POS_NAME1" = "Apple Tester";
            };
            TC2 =                 {
                "CSHR_NUM" = 8;
                "CSHR_NUM1" = 8;
                "CSHR_POS_NAME" = Jonathan;
                "CSHR_POS_NAME1" = Jonathan;
            };
            TC3 =                 {
                "CSHR_NUM" = 54;
                "CSHR_NUM1" = 54;
                "CSHR_POS_NAME" = LEXIE;
                "CSHR_POS_NAME1" = LEXIE;
            };
            TC4 =                 {
                "CSHR_NUM" = 7;
                "CSHR_NUM1" = 7;
                "CSHR_POS_NAME" = Randy;
                "CSHR_POS_NAME1" = Randy;
            };
        };
    };
    schema =         {
        element =             {
            complexType =                 {
                choice =                     {
                    element =                         {
                        complexType =                             {
                            sequence =                                 {
                                element = 0;
                                element1 = "<null>";
                                element2 = 0;
                            };
                            sequence1 =                                 {
                                element = 0;
                                element1 = "<null>";
                                element2 = 0;
                            };
                        };
                        complexType1 =                             {
                            sequence =                                 {
                                element = 0;
                                element1 = "<null>";
                                element2 = 0;
                            };
                            sequence1 =                                 {
                                element = 0;
                                element1 = "<null>";
                                element2 = 0;
                            };
                        };
                    };
                    element1 =                         {
                        complexType =                             {
                            sequence =                                 {
                                element = 0;
                                element1 = "<null>";
                                element2 = 0;
                            };
                            sequence1 =                                 {
                                element = 0;
                                element1 = "<null>";
                                element2 = 0;
                            };
                        };
                        complexType1 =                             {
                            sequence =                                 {
                                element = 0;
                                element1 = "<null>";
                                element2 = 0;
                            };
                            sequence1 =                                 {
                                element = 0;
                                element1 = "<null>";
                                element2 = 0;
                            };
                        };
                    };
                };
                choice1 =                     {
                    element =                         {
                        complexType =                             {
                            sequence =                                 {
                                element = 0;
                                element1 = "<null>";
                                element2 = 0;
                            };
                            sequence1 =                                 {
                                element = 0;
                                element1 = "<null>";
                                element2 = 0;
                            };
                        };
                        complexType1 =                             {
                            sequence =                                 {
                                element = 0;
                                element1 = "<null>";
                                element2 = 0;
                            };
                            sequence1 =                                 {
                                element = 0;
                                element1 = "<null>";
                                element2 = 0;
                            };
                        };
                    };
                    element1 =                         {
                        complexType =                             {
                            sequence =                                 {
                                element = 0;
                                element1 = "<null>";
                                element2 = 0;
                            };
                            sequence1 =                                 {
                                element = 0;
                                element1 = "<null>";
                                element2 = 0;
                            };
                        };
                        complexType1 =                             {
                            sequence =                                 {
                                element = 0;
                                element1 = "<null>";
                                element2 = 0;
                            };
                            sequence1 =                                 {
                                element = 0;
                                element1 = "<null>";
                                element2 = 0;
                            };
                        };
                    };
                };
            };
            complexType1 =                 {
                choice =                     {
                    element =                         {
                        complexType =                             {
                            sequence =                                 {
                                element = 0;
                                element1 = "<null>";
                                element2 = 0;
                            };
                            sequence1 =                                 {
                                element = 0;
                                element1 = "<null>";
                                element2 = 0;
                            };
                        };
                        complexType1 =                             {
                            sequence =                                 {
                                element = 0;
                                element1 = "<null>";
                                element2 = 0;
                            };
                            sequence1 =                                 {
                                element = 0;
                                element1 = "<null>";
                                element2 = 0;
                            };
                        };
                    };
                    element1 =                         {
                        complexType =                             {
                            sequence =                                 {
                                element = 0;
                                element1 = "<null>";
                                element2 = 0;
                            };
                            sequence1 =                                 {
                                element = 0;
                                element1 = "<null>";
                                element2 = 0;
                            };
                        };
                        complexType1 =                             {
                            sequence =                                 {
                                element = 0;
                                element1 = "<null>";
                                element2 = 0;
                            };
                            sequence1 =                                 {
                                element = 0;
                                element1 = "<null>";
                                element2 = 0;
                            };
                        };
                    };
                };
                choice1 =                     {
                    element =                         {
                        complexType =                             {
                            sequence =                                 {
                                element = 0;
                                element1 = "<null>";
                                element2 = 0;
                            };
                            sequence1 =                                 {
                                element = 0;
                                element1 = "<null>";
                                element2 = 0;
                            };
                        };
                        complexType1 =                             {
                            sequence =                                 {
                                element = 0;
                                element1 = "<null>";
                                element2 = 0;
                            };
                            sequence1 =                                 {
                                element = 0;
                                element1 = "<null>";
                                element2 = 0;
                            };
                        };
                    };
                    element1 =                         {
                        complexType =                             {
                            sequence =                                 {
                                element = 0;
                                element1 = "<null>";
                                element2 = 0;
                            };
                            sequence1 =                                 {
                                element = 0;
                                element1 = "<null>";
                                element2 = 0;
                            };
                        };
                        complexType1 =                             {
                            sequence =                                 {
                                element = 0;
                                element1 = "<null>";
                                element2 = 0;
                            };
                            sequence1 =                                 {
                                element = 0;
                                element1 = "<null>";
                                element2 = 0;
                            };
                        };
                    };
                };
            };
        };
        element1 =             {
            complexType =                 {
                choice =                     {
                    element =                         {
                        complexType =                             {
                            sequence =                                 {
                                element = 0;
                                element1 = "<null>";
                                element2 = 0;
                            };
                            sequence1 =                                 {
                                element = 0;
                                element1 = "<null>";
                                element2 = 0;
                            };
                        };
                        complexType1 =                             {
                            sequence =                                 {
                                element = 0;
                                element1 = "<null>";
                                element2 = 0;
                            };
                            sequence1 =                                 {
                                element = 0;
                                element1 = "<null>";
                                element2 = 0;
                            };
                        };
                    };
                    element1 =                         {
                        complexType =                             {
                            sequence =                                 {
                                element = 0;
                                element1 = "<null>";
                                element2 = 0;
                            };
                            sequence1 =                                 {
                                element = 0;
                                element1 = "<null>";
                                element2 = 0;
                            };
                        };
                        complexType1 =                             {
                            sequence =                                 {
                                element = 0;
                                element1 = "<null>";
                                element2 = 0;
                            };
                            sequence1 =                                 {
                                element = 0;
                                element1 = "<null>";
                                element2 = 0;
                            };
                        };
                    };
                };
                choice1 =                     {
                    element =                         {
                        complexType =                             {
                            sequence =                                 {
                                element = 0;
                                element1 = "<null>";
                                element2 = 0;
                            };
                            sequence1 =                                 {
                                element = 0;
                                element1 = "<null>";
                                element2 = 0;
                            };
                        };
                        complexType1 =                             {
                            sequence =                                 {
                                element = 0;
                                element1 = "<null>";
                                element2 = 0;
                            };
                            sequence1 =                                 {
                                element = 0;
                                element1 = "<null>";
                                element2 = 0;
                            };
                        };
                    };

                        complexType1 =                             {
                            sequence =                                 {
                                element = 0;
                                element1 = "<null>";
                                element2 = 0;
                            };
                            sequence1 =                                 {
                                element = 0;
                                element1 = "<null>";
                                element2 = 0;
                            };
                        };
                    };
                    element1 =                         {
                        complexType =                             {
                            sequence =                                 {
                                element = 0;
                                element1 = "<null>";
                                element2 = 0;
                            };
                            sequence1 =                                 {
                                element = 0;
                                element1 = "<null>";
                                element2 = 0;
                            };
                        };
                        complexType1 =                             {
                            sequence =                                 {
                                element = 0;
                                element1 = "<null>";
                                element2 = 0;
                            };
                            sequence1 =                                 {
                                element = 0;
                                element1 = "<null>";
                                element2 = 0;
                            };
                        };
                    };
                };
            };
        };
    };

我无法阻止它多次提取数据,并提取所有 ? 当孩子不是1时,代码不应该停止吗?

4

1 回答 1

0

元素必须放在调用[d setObject:v forKey:[child name]]之上;并且必须添加 else 以便 d 不会被覆盖。

if( check != nil ) {              
NSInteger next = 1;              
key = [NSString stringWithFormat:@"%@%04d", [child name], next]; 
check = [d objectForKey:key];  
while( check != nil ) {                  
    next++;  
    key = [NSString stringWithFormat:@"%@%04d", [child name], next];  
    check = [d objectForKey:key];  
}  
[d setObject:v forKey:key];  
} else { 
[d setObject:v forKey:[child name]]; 
} 

否则 'd' 中的元素将被覆盖,因为 setObject 被调用了两次。

于 2012-08-02T15:16:08.620 回答