本文介绍了xml数据岛的解决方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我最近继承了一个巨大的webapp,它是JSP,Javascript和Java的组合。它仅适用于IE,因为它使用xml数据岛编码的方式以及其他妨碍其他浏览器平滑运行的功能。一切都很好,直到几天后,Windows 7盒已推出给几个用户,其中IE9 / 10在应用程序中的某些javascript中遇到了麻烦。例如,以下数据岛是我的html页面的一个片段。 < xml id =underlyingdataondatasetcomplete = window.dialogArguments.parent.repopulateDropDown(this,underlyingdd)> < / xml> < xml id =termdataondatasetcomplete =window.dialogArguments.parent.repopulateDropDown(this,termdd)> < / xml> 在此页面上还有另一行代码 window.dialogArguments.parent.request(underlyingdata,CONTRACT.LIST.WB,PULP AND PAPER |+ instrumentdd.options [instrumentdd.selectedIndex] .text); 调用一个如下函数 函数请求(xmldataisland,requestmethod,参数 { var screwcache = Math.round(Math.random()* 10000); xmldataisland。 value = null; xmldataisland.load(/ webaccess / Request?sessionid =+ sessionid +& request" + requestmethod +"&参数=+ parameters +& screwcache =+ screwcache) ; } 在IE9 / 10中失败, load'在'xmldataisland'对象上不是一个有效的方法(脚本438错误),而它在IE 5到IE 8上完全正常。 我相信xmldataisland对象在上面的函数中是XMLDocument类型的,为什么load方法失败了?对此有什么解决方法?我从很多来源读到并听到使用数据岛是一个可怕的想法,在这种情况下,正确的替代方案是什么? ?解决方从IE10开始, XML数据岛屿不再被支持 - 浏览器将它们解析为HTML。 Mozilla开发者网络制作了文章,它为XML数据岛提供了一个跨浏览器的替代方案,即HTML5数据块。本文演示如果省略 src 属性,则可以将< script> 元素用作数据块,并且类型属性不指定可执行的脚本类型。您还必须确保嵌入式XML内容不包含< / script> 标签。 来源: https://developer.mozilla.org/en/docs/Using_XML_Data_Islands_in_Mozilla 以下是他们给出的HTML示例... <!DOCTYPE html> < html> < head> < title> XML数据模块演示< / title> < script id =purchase-ordertype =application / xml> < purchaseOrder xmlns =http://example.mozilla.org/PurchaseOrderML> < lineItem> <名称>订单项1< / name> <价格> 1.25< /价格> < / lineItem> < lineItem> <名称>订单项2< / name> <价格> 2.48< / price> < / lineItem> < / purchaseOrder> < / script> < script> 函数runDemo(){ //原始XML数据可以用这个来检索... var orderSource = document.getElementById(purchase-order)。textContent; //使用DOMParser API可以将XML数据解析为DOM树... var parser = new DOMParser(); var doc = parser.parseFromString(orderSource,application / xml); var lineItems = doc.getElementsByTagNameNS(http://example.mozilla.org/PurchaseOrderML,lineItem); var firstPrice = lineItems [0] .getElementsByTagNameNS(http://example.mozilla.org/PurchaseOrderML,price)[0] .textContent; document.body.textContent =采购订单包含+ lineItems.length +行项目,第一行项目的价格为+ firstPrice +。 } < / script> < / head> < body onload =runDemo();> Demo未执行< / body> < / html> 您需要编写自己的方法将数据加载到该块中(可能使用 jQuery.get 或 .load 方法)。 希望有帮助! I recently inherited a huge webapp which is a combination of JSP,Javascript and Java. It works only on IE due to the way it has been coded using xml data islands and other things which prevent smooth functioning on other browsers. Everything was fine until a few days when Windows 7 boxes were rolled out for a few users in which IE9/10 has trouble with some of the javascript in the application. For example, the following data island is a snippet from my html page.<xml id = "underlyingdata" ondatasetcomplete="window.dialogArguments.parent.repopulateDropDown(this, underlyingdd)"></xml><xml id="termdata" ondatasetcomplete="window.dialogArguments.parent.repopulateDropDown(this, termdd)"></xml>On this page there is another line of code window.dialogArguments.parent.request(underlyingdata, "CONTRACT.LIST.WB", "PULP AND PAPER|" + instrumentdd.options[instrumentdd.selectedIndex].text);that calls a function which is as follows function request(xmldataisland, requestmethod, parameters{ var screwcache=Math.round(Math.random()*10000); xmldataisland.value=null; xmldataisland.load("/webaccess/Request?sessionid=" + sessionid + "&request=" + requestmethod + "&parameters=" + parameters+"&screwcache="+screwcache);}This fails in IE9/10 with the error that 'load' is not a valid method(Script 438 error) on 'xmldataisland' object, whereas it works perfectly fine on IE 5 to IE 8.I believe the xmldataisland object in the above function is of type XMLDocument. Why does the load method fail? What is the workaround for this? I read and hear from many sources that using data islands is a terrible idea. What would be the correct alternative for this in that case? 解决方案 From IE10 onwards, XML data islands are no longer supported - the browser parses them as HTML. The Mozilla Developer Network have produced an article which gives a cross-browser alternative to XML data islands, namely an HTML5 "data block". The article demonstrates that a <script> element can be used as a data block if the src attribute is omitted and the type attribute does not specify an executable script type. You must also ensure that the embedded XML content doesn't include a </script> tag.Source: https://developer.mozilla.org/en/docs/Using_XML_Data_Islands_in_MozillaHere's the HTML example they give...<!DOCTYPE html><html><head><title>XML Data Block Demo</title><!-- this is the data block which contains the XML data --><script id="purchase-order" type="application/xml"><purchaseOrder xmlns="http://example.mozilla.org/PurchaseOrderML"> <lineItem> <name>Line Item 1</name> <price>1.25</price> </lineItem> <lineItem> <name>Line Item 2</name> <price>2.48</price> </lineItem></purchaseOrder></script><script>function runDemo() { // the raw XML data can be retrieved using this... var orderSource = document.getElementById("purchase-order").textContent; // the XML data can be parsed into a DOM tree using the DOMParser API... var parser = new DOMParser(); var doc = parser.parseFromString(orderSource, "application/xml"); var lineItems = doc.getElementsByTagNameNS("http://example.mozilla.org/PurchaseOrderML", "lineItem"); var firstPrice = lineItems[0].getElementsByTagNameNS("http://example.mozilla.org/PurchaseOrderML", "price")[0].textContent; document.body.textContent = "The purchase order contains " + lineItems.length + " line items. The price of the first line item is " + firstPrice + ".";}</script></head><body onload="runDemo()";>Demo did not run</body></html>You will need to write your own method to load data into that block (perhaps using jQuery.get or .load methods).Hope that helps! 这篇关于xml数据岛的解决方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-28 16:39