将json对象转化为xml、soap字符串

<script>
// 将json对象转化为xml
function toXml(obj){
    var xml = "";
    for(var k in obj){
        var v = obj[k];
        if(typeof(v)!="object"){
            xml += "<" + k + ">" + v + "</" + k + ">";
        }else{
            xml += "<" + k + ">";
            xml += toXml(v);
            xml += "</" + k + ">";
        }
    }
    return xml;
}

// 构建soap调用字符串
// name : 方法名称
// args : 附属json参数
// ns   : 方法的namespace
function toSoap(name, args, ns)
{
  var msg = '';
  msg += '<v:Envelope\r\n';
  msg += '  xmlns:i="http://www.w3.org/2001/XMLSchema-instance%22/r/n';
  msg += '  xmlns:d="http://www.w3.org/2001/XMLSchema%22/r/n';
  msg += '  xmlns:c="http://schemas.xmlsoap.org/soap/encoding/%22/r/n';
  msg += '  xmlns:v="http://schemas.xmlsoap.org/soap/envelope/%22/r/n';
  msg += '  >\r\n';
  msg += '  <v:Header/>\r\n';
  msg += '  <v:Body>\r\n';
  msg += '    <' + name + ' xmlns="' + ns + '" id="o0" c:root="1">\r\n';
  msg += '      ' + toXml(args) + "\r\n";
  msg += '    </' + name + '>\r\n';
  msg += '  </v:Body>\r\n';
  msg += '</v:Envelope>\r\n';
  return msg;
}

//var o = {a:1, b:"xx", c:{c1:3, c2:"ww"}};
//alert(toXml(o));
var o = {
  zi_tid:'XQ9Fasfi+7wBswdfgsszCw==',
  zi_isre:1,
  key:null,
  row:10,
  skip:0,
  code:'iJRgfsfsjQyO4'
  };
var soap = toSoap('ListInfo', o, 'AuthorityServicewsdl');
alert(soap);
</script>

ps. 这段脚本在调用php的webservice时会用到(用标准的soap协议),aspnet的webservice既实现了标准的soap也实现了普通的get/post方式,调用简单很多。

转载请注明出处:http://surfsky.cnblogs.com 

原文地址:https://www.cnblogs.com/surfsky/p/2721747.html