1、代码
//加载xml数据function loadXml(str) {if (str &#61;&#61; null) {return null;}var doc &#61; str;try{doc &#61; createXMLDOM();doc.async &#61; false;doc.loadXML(str);}catch(e){doc &#61; $.parseXML(str);}return doc;}//xml数据转成json对象的数据function xmlTojson(xmlObj, nodename, isarray){var obj &#61; $(xmlObj);var itemobj &#61; {};var nodenames &#61; "";var getAllAttrs &#61; function(node){//递归解析xml 转换成json对象var _itemobj &#61; {};var notNull &#61; false;var nodechilds &#61; node.childNodes;var childlenght &#61; nodechilds.length;var _attrs &#61; node.attributes;var firstnodeName &#61; "#text";try{firstnodeName &#61; nodechilds[0].nodeName;}catch(e){}if((childlenght > 0 && firstnodeName !&#61; "#text") || _attrs.length > 0){var _childs &#61; nodechilds;var _childslength &#61; nodechilds.length;var _fileName_ &#61; "";if(undefined !&#61; _attrs){var _attrslength &#61; _attrs.length;for(var i &#61; 0; i <_attrslength; i&#43;&#43;){//解析xml节点属性var attrname &#61; _attrs[i].nodeName;var attrvalue &#61; _attrs[i].nodeValue;_itemobj[attrname] &#61; attrvalue;}}for (var j &#61; 0; j <_childslength; j&#43;&#43;) {//解析xml子节点var _node &#61; _childs[j];var _fildName &#61; _node.nodeName;if("#text" &#61;&#61; _fildName){break;};if(_itemobj[_fildName] !&#61; undefined){//如果有重复的节点需要转为数组格式if(!(_itemobj[_fildName] instanceof Array)){var a &#61; _itemobj[_fildName];_itemobj[_fildName] &#61; [a];//如果该节点出现大于一个的情况 把第一个的值存放到数组中
}}var _fildValue &#61; getAllAttrs(_node);try{_itemobj[_fildName].push(_fildValue);}catch(e){_itemobj[_fildName] &#61; _fildValue;_itemobj["length"] &#61; 1;}}}else{_itemobj &#61; (node.textContent &#61;&#61; undefined) ? node.text : node.textContent;}return _itemobj;};if(nodename){nodenames &#61; nodename.split("/")}for(var i &#61; 0;i
2、测试结果