0

我有这样的回应:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body>
      <ns2:SurfaceElementListResponse xmlns:ns2="http://isiows.isiom.fr/schema/DmdCreation" xmlns:ns3="http://isiows.isiom.fr/schema/Error" xmlns:ns4="http://isiows.isiom.fr/schema/DmdDetail">
         <SurfaceElementListResult>
            <idSurfaceElement>9482</idSurfaceElement>
            <name>R04-</name>
            <type>NIVEAU</type>
         </SurfaceElementListResult>
         <SurfaceElementListResult>
            <idSurfaceElement>9486</idSurfaceElement>
            <name>Zone A</name>
            <type>ZONE</type>
         </SurfaceElementListResult>
      </ns2:SurfaceElementListResponse>
   </soap:Body>
</soap:Envelope>

我正在等待将每个对象反序列化为 NSDictionary,因为它适用于除上述之外的所有其他 WS 响应。与其他响应相比,在 SOAP.m:+ (id) deserialize: (CXMLNode*) element方法中,语句的所有响应都NSString* type = [Soap getNodeValue:element withName:@"type"];返回 nil,所以它继续返回[Soap deserializeAsDictionary:element];,我得到了必要的结果。在我的情况下,当我到达NSString* type = [Soap getNodeValue:element withName:@"type"];该语句时,第一个对象返回“NIVEAU”,另一个对象返回“ZONE”,这不允许应用程序运行并执行[Soap deserializeAsDictionary:element];,我得到一个字符串对象而不是 NSDictionary 作为解析结果。

你能帮我解决这个问题吗?

4

1 回答 1

2

// Deserialize an object as a generic object
+ (id) deserialize: (CXMLNode*) element{

    // Get the type
    NSString* type = [Soap getNodeValue:element withName:@"type"];

调查 [Soap getNodeValue]... 我发现,这个函数的使用太笼统了。它用于获取字段的属性,作为字段的值。

// Gets the value of a named node from a parent node.

+ (NSString*) getNodeValue: (CXMLNode*) node withName: (NSString*) name {

    // Set up the variables
    if(node == nil || name == nil) { return nil; }
    CXMLNode* child = nil;

    // If it's an attribute get it
    if([node isKindOfClass: [CXMLElement class]])
    {
        child = [(CXMLElement*)node attributeForName: name];
        if(child != nil) {
            return [child stringValue];
        }
    }

    // Otherwise get the first element
    child = [Soap getNode: node withName: name];
    if(child != nil) {
        return [child stringValue];
    }
    return nil;
}

在这种情况下,我创建了一个只获取属性值的新方法:

// Gets the value of a node attribute
+ (NSString*) getNodeAttributeValue: (CXMLNode*) node withName: (NSString*) name{
    // Set up the variables
    if(node == nil || name == nil) { return nil; }
    CXMLNode* child = nil;

    // If it's an attribute get it
    if([node isKindOfClass: [CXMLElement class]])
    {
        child = [(CXMLElement*)node attributeForName: name];
        if(child != nil) {
            return [child stringValue];
        }
    }
    return nil;
}

并在 Soap:deserialize 中替换它:

// Deserialize an object as a generic object
+ (id) deserialize: (CXMLNode*) element{

    // Get the type
    NSString* type = [Soap getNodeAttributeValue:element withName:@"type"];
//  NSString* type = [Soap getNodeValue:element withName:@"type"];
    if(type == nil || type.length == 0) {

到目前为止,它对我来说非常有效。希望它能帮助其他有同样情况的人。

于 2012-08-20T08:00:01.463 回答