2
Vote

_node2object overwrites objects with the same tag name

description

Assuming the following xml:
<Data>
<row>a</row>
<row>b</row>
<row>c</row>
</Data>
resulted object will be
{Data: {row: c}}
 
The following part of _node2object function must be modified:
if(!isarray)
{
    var obj = null;
    if(node.hasChildNodes())
        obj = new Object();
    for(var i = 0; i < node.childNodes.length; i++)
    {
        var p = SOAPClient._node2object(node.childNodes[i], wsdlTypes);
        obj[node.childNodes[i].nodeName] = p;
    }
    return obj;
}
 
A possible solution can be (which works for me, but not tested very well):
if(!isarray)
{
    var obj = node.hasChildNodes() ? {} : null;
    for(var i = 0; i < node.childNodes.length; i++)
    {
        var p = SOAPClient._node2object(node.childNodes[i], wsdlTypes);
        var cname = node.childNodes[i].nodeName;
        if (obj[cname]) {
            if (obj[cname] instanceof Array) {
                obj[cname].push(p);
            }
            else {
                obj[cname] = new Array(obj[cname]);
            }
        }
        else {
            obj[cname] = p;
        }
    }
    return obj;
}
 
The result will be: {Data: {row: [a, b, c]}}

comments

stefano_mora wrote Sep 1, 2015 at 10:19 AM

Hi,
thanks for the solution but I think it is not complete: it is missing the second element of an array.
In particular I modified the code in this way:
       ...
          else {
             obj[cname] = new Array(obj[cname]); // re-create tha variable as array with the pre-existent item
             obj[cname] = p;  // insert the current item
         }
     ...
Regards