在项目中用到类似结构的XML文件
<?xml version="1.0" encoding="utf-8"?>
<root>
<record>
<AssetName>test1</AssetName>
<AssetBrand>test1</AssetBrand>
<AssetStandard>test1</AssetStandard>
<AssetMount>test1</AssetMount>
<AssetSuppliers>test1</AssetSuppliers>
<SpecialRequirements>test1</SpecialRequirements>
<InstallSite>test1</InstallSite>
<ApplyOrShift>采购</ApplyOrShift>
<Configuration>test1</Configuration>
<Register>确认到货</Register>
</record>
</root>
<root>
<record>
<AssetName>test1</AssetName>
<AssetBrand>test1</AssetBrand>
<AssetStandard>test1</AssetStandard>
<AssetMount>test1</AssetMount>
<AssetSuppliers>test1</AssetSuppliers>
<SpecialRequirements>test1</SpecialRequirements>
<InstallSite>test1</InstallSite>
<ApplyOrShift>采购</ApplyOrShift>
<Configuration>test1</Configuration>
<Register>确认到货</Register>
</record>
</root>
用
$(xml).find("record").each(function() {
var ele = $(this);
var ele = $(this);
……
}进行解析的时候,在火狐、chrome等能够正常解析,但是在IE中却提示错误,在
http://www.newmediafun.com/2009/07/parsing-xml-with-jquery-in-internet-explorer/上找到解决办法
$.
ajax
(
{
url : 'xml/myXML.xml' ,
dataType : ($. browser. msie ) ? "text" : "xml" ,
timeout : 1000 ,
error : function (data ) {
alert ( 'Error occurred loading the XML' ) ;
} ,
success : function (data ) {
var xml ;
if ( typeof data == "string" ) {
xml = new ActiveXObject ( "Microsoft.XMLDOM" ) ;
xml. async = false ;
xml. loadXML (data ) ;
} else {
xml = data ;
}
// parse the xml
} ) ;
url : 'xml/myXML.xml' ,
dataType : ($. browser. msie ) ? "text" : "xml" ,
timeout : 1000 ,
error : function (data ) {
alert ( 'Error occurred loading the XML' ) ;
} ,
success : function (data ) {
var xml ;
if ( typeof data == "string" ) {
xml = new ActiveXObject ( "Microsoft.XMLDOM" ) ;
xml. async = false ;
xml. loadXML (data ) ;
} else {
xml = data ;
}
// parse the xml
} ) ;
根据该方法的修改
var xml;
if (jQuery.browser.msie) {
xml = new ActiveXObject("Microsoft.XMLDOM");
xml.async = false;
xml.loadXML(test);
}
else {
xml = test;
}
$(xml).find("record").each(function() {
var ele = $(this);
……
}
另外,针对有的网友提出的在root节点外面再套一层<xml></xml>的方法也试过,在root下存在两个及其以上节点时没有问题,但是在root下只有一个rocord节点的时候还是有问题。所以还是推荐上面这种方法。不过在IE7以下的浏览器中,还需要完善xml对象的创建方法。