您的当前位置:首页正文

JavaScript将XML转成JSON的方法_javascript技巧

2020-11-27 来源:华拓教育

本文实例讲述了JavaScript将XML转成JSON的方法。分享给大家供大家参考。具体方法如下:

1. JavaScript代码如下:
代码如下:// Changes XML to JSON
function xmlToJson(xml) {
// Create the return object
var obj = {};
if (xml.nodeType == 1) { // element
// do attributes
if (xml.attributes.length > 0) {
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) { // text
obj = xml.nodeValue;
}
// do children
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] = xmlToJson(item);
} else {
if (typeof(obj[nodeName].length) == "undefined") {
var old = obj[nodeName];
obj[nodeName] = [];
obj[nodeName].push(old);
}
obj[nodeName].push(xmlToJson(item));
}
}
}
return obj;
};
2. XML代码:
代码如下:

<br /> <LINKSIN NUM="1102"/><br /> <SPEED TEXT="1421" PCT="51"/><br /> </SD><br /> <SD><br /> <POPULARITY URL="davidwalsh.name/" TEXT="7131"/><br /> <REACH RANK="5952"/><br /> <RANK DELTA="-1648"/><br /> </SD><br /> <br /> 3. JSON结果:<br /> 代码如下:{<br /> "@attributes": {<br /> AID: "=",<br /> HOME: 0,<br /> URL: "davidwalsh.name/",<br /> VER: "0.9",<br /> },<br /> SD = [<br /> {<br /> "@attributes": {<br /> FLAGS: "",<br /> HOST: "davidwalsh.name",<br /> TITLE: A<br /> },<br /> LINKSIN: {<br /> "@attributes": {<br /> NUM: 1102<br /> }<br /> },<br /> SPEED: {<br /> "@attributes": {<br /> PCT: 51,<br /> TEXT: 1421<br /> }<br /> },<br /> TITLE: {<br /> "@attributes": {<br /> TEXT: "David Walsh Blog :: PHP, MySQL, CSS, Javascript, MooTools, and Everything Else",<br /> }<br /> },<br /> },<br /> {<br /> POPULARITY: {<br /> "@attributes": {<br /> TEXT: 7131,<br /> URL: "davidwalsh.name/"<br /> }<br /> },<br /> RANK: {<br /> "@attributes": {<br /> DELTA: "-1648"<br /> }<br /> },<br /> REACH: {<br /> "@attributes": {<br /> RANK = 5952<br /> }<br /> }<br /> }<br /> ]<br /> }</p> <p>关于js操作xml感兴趣的朋友还可参考在线工具:</p> <p>在线XML/JSON互相转换工具<br /> </p> <p>在线XML格式化/压缩工具<br /> </p> <p>希望本文所述对大家的javascript程序设计有所帮助。 <script type="text/javascript" src="https://jss.huatuo6.com/pc/wenzhang/detail_dy.js"></script> <script type="text/javascript" src="https://jss.huatuo0.cn/pc/wenzhang/detail_left.js"></script> </div> <div class="content_fy"> </div> </div> <script type="text/javascript" src="https://jss.huatuo0.cn/pc/wenzhang/detail_foot.js"></script> <script type="text/javascript" src="https://jss.huatuo0.cn/pc/share_right_gg1.js"></script> <script type="text/javascript" src="https://jss.huatuo6.com/pc/share_right_xgzx.js"></script> <script type="text/javascript" src="https://jss.huatuo0.cn/pc/share_right_gg2.js"></script> <script type="text/javascript" src="https://jss.huatuo6.com/pc/share_right_rmyd.js"></script> </div> <div class="n_right"> <script type="text/javascript" src="https://jss.huatuo0.cn/pc/share_cebian_gg1.js"></script> <script type="text/javascript" src="https://jss.huatuo6.com/pc/share_cebian_rmht.js"></script> <script type="text/javascript" src="https://jss.huatuo0.cn/pc/share_cebian_gg2.js"></script> <script type="text/javascript" src="https://jss.huatuo6.com/pc/share_cebian_rmtw.js"></script> <script type="text/javascript" src="https://jss.huatuo0.cn/pc/share_cebian_gg3.js"></script> <script type="text/javascript" src="https://jss.huatuo6.com/pc/share_cebian_wntj.js"></script> </div> </div> <div class="footer"> <div class="m_box"> <div class="footer_co"> <p>Copyright © 2019-<span class="currentYear"></span> huatuo0.cn 版权所有 <br> <a style="color:#fff" href="https://beian.miit.gov.cn/" target="_blank">湘ICP备2023017654号-2</a></p> </div> </div> <a href="#0" class="cd-top">Top</a> <script type="text/javascript" src="https://jss.huatuo0.cn/pc/foot_foot.js"></script> <script type="text/javascript"> jQuery(".focusBox").hover(function () { jQuery(this).find(".prev,.next").stop(true, true).fadeTo("show", 0.2) }, function () { jQuery(this).find(".prev,.next").fadeOut() }); jQuery(".focusBox").slide({mainCell: ".pic", effect: "fold", autoPlay: true, delayTime: 600, trigger: "click"}); </script> <script type="text/javascript"> jQuery(".coll_list li").hover(function () { jQuery(this).addClass("on").siblings().removeClass("on") }, function () { }); jQuery(".coll-rank").slide({titCell: "dt h3", mainCell: "dd", autoPlay: false, effect: "left", delayTime: 300}); </script> <script type="text/javascript"> $(function () { var _line = parseInt($(window).height() / 3); $(window).scroll(function () { if ($(window).scrollTop() > 75) { $('.i_sidemenu').css({'position': 'fixed', 'top': '75px'}) } else { $('.i_sidemenu').css({'position': '', 'top': ''}) } ; }); }); $(function () { var _line = parseInt($(window).height() / 3); $(window).scroll(function () { if ($(window).scrollTop() > 1000) { $('.right_a').css({'position': 'fixed', 'top': '75px'}) } else { $('.right_a').css({'position': '', 'top': ''}) } ; }); }); </script> <script type="text/javascript"> const currentYear = new Date().getFullYear(); $('.currentYear').html(currentYear) </script> <script type="text/javascript" src="https://jss.huatuo6.com/pc/tj_foot1.js"></script> </body> </html>