关于javascript:Grunt混淆代码报-XML-DOMParser-错误

33次阅读

共计 1719 个字符,预计需要花费 5 分钟才能阅读完成。

我的项目在浏览器中可失常运行,然而 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
                        });
                    }

正文完
 0