项目在浏览器中可正常运行,但是Grunt混淆时报错。
介绍中说是IE 不支持 DOMParser 对象。IE中使用 Document.loadXML()的 XML 解析。
于是改为Document.loadXML方法解决。
怀疑Grunt不支持DOMParser方法

_Widget.xml = (function($) {
   var xml = {
      xmlToJson: function(xml, key) {
         var childNodes, result;
         if(key) {
            var keys = key.split("->");
            for(var i = 0; i < keys.length; i++) {
               if(i == 0) {
                  childNodes = xml.getElementsByTagName(keys[i])[0];
                } else {
                  childNodes = childNodes.getElementsByTagName(keys[i])[0]
               }
            }
         } else {
            childNodes = xml;
         }
         xml = childNodes;
         return parse(xml);
         function parse(xml) {
            var obj = {};
             if(xml.nodeType == 1) { // element
                  obj["@attributes"] = {};
                    for(var j = 0; j < xml.attributes.length; j++) {
                         var attribute = xml.attributes.item(j);
                         obj["@attributes"][attribute.nodeName] = attribute.nodeValue;
            }
               }
            } else if(xml.nodeType == 3) { 
                obj = xml.nodeValue;
            }
          
            if(xml.hasChildNodes()) {
               for(var i = 0; i < xml.childNodes.length; i++) {
                  var item = xml.childNodes.item(i);
                  var nodeName = item.nodeName;
                  if(typeof(obj[nodeName]) == "undefined") {
                     obj[nodeName] = parse(item);
                  } else {
                     if(typeof(obj[nodeName].length) == "undefined") {
                        var old = obj[nodeName];
                        obj[nodeName] = [];
                        obj[nodeName].push(old);
                }
                     obj[nodeName].push(parse(item));
                }
               }
            }
            return obj;
 }
      },
    jsonToxml: function(json){
         if(document.all){
            var xmlDom = new ActiveXObject("Microsoft.XMLDOM");
            xmlDom.loadXML(json);
            return xmlDom;
          }else{
            return new DOMParser().parseFromString(json, "text/xml");
          }
      }
   };
 return xml;
})(Zepto);

调用:

                    var xml = _Widget.xml.jsonToxml(res.msg);
                    var json = _Widget.xml.xmlToJson(xml);
                    var status = json["soap:Envelope"]["soap:Body"]["DoActionResponse"]["DoActionResult"]["#text"];
                    var text = json["soap:Envelope"]["soap:Body"]["DoActionResponse"]["ls_output"]["#text"];
                    if(status == 1){
                        waitHandleView.EventDataChanged.notifyEvent(waitHandleView, {
                            "data": JSON.parse(text)
                        });
                        NavigationView.pushView("waitHandleView")
                    }else{
                        _Widget.load.show({
                            txt: text || "没有获取到患者信息",
                            top: 640,
                            time: 5
                        });
                    }

大象
211 声望6 粉丝

下一篇 »
ES6 Array.form