-1

I have data coming in XML file, and initially i was using jQuery Ajax function to read and process data in XML file... whole functionality works perfectly until i have tried on IE 9 browser and have so many different solution but is just not read data through XML file.I am using data type ($.browser.msie) ? "text" and xml for rest of browser, followed by i am calling parseXml() for IE but is just not happening .... I am really struggling and thinking to change other possible method that is suitable for all!!!

 function testXml() {

    $.ajax({
        type: 'GET',
        url: 'XML_estatesIT_op4.xml',
        dataType: ($.browser.msie) ? "text" : "xml",
        success: function (xml) {

            theXml = parseXml(xml);

            $(theXml).find("property").each(function () {

                var b1 = $(this).find('proptype').text();

                alert(b1);                        
            });
        },
        error: function () {
            alert("An error occurred while processing XML file.");
        }
     });
 }

 function parseXml(xml) {

    if (jQuery.browser.msie) {
        var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async = false;
        xmlDoc.loadXML(xml);
        xml = xmlDoc;
    }

    return xml;
}

I am wondering if i can read

  1. xml data in ajax function
  2. if it success, convert xmlDocument object into JSON
  3. then process on data, so that i can read in IE and other browsers...

I havn't use JSON, can anyone please guide me if i can do that!!

many thanks

4

1 回答 1

0

最后我找到了解决方案,诀窍是为版本小于 10 的 IE 浏览器使用单独的代码 XML。

所以每次调用 Ajax 时都会使用输入参数 XML Dom 或文本调用方法 parseXml,具体取决于浏览器....如果当前浏览器是 IE,它会上传 XML 文档,根据 Microsoft 标准对其进行处理并返回 XML 和其余部分Ajax 中的进程按预期进行!

注意:jQuery 1.9 不支持 browser.msie,但您可以添加 jquery-migrate-1.2.1.min.js 以使其兼容或使用 userAgent 并查找当前浏览器

  $.ajax({
      type: 'GET',
      url: 'XML_file.xml',
      dataType: ($.browser.msie) ? "text" : "xml",
      success: function (xml) {

         var processedXML = parseXml(xml);

         $(processedXML).find('my record').each(function () {  //code  } 
  });


  function parseXml(xml) {

  if ($.browser.msie)  {

    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

        xmlhttp.open("GET", "XML_file.xml", false);
        xmlhttp.send();
        xmlDoc = xmlhttp.responseXML;

        xml = xmlDoc;
  }
  return xml;
}

我最初的问题在这里得到了回答,我问我是否可以将 xml 转换为 json,是的,你可以;一旦调用成功方法... xml文档下来了,您可以使用 xml-t0-json 插件来做到这一点...

于 2013-06-15T14:42:57.850 回答