首页 热点资讯 义务教育 高等教育 出国留学 考研考公
您的当前位置:首页正文

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程序设计有所帮助。 </mip-showmore> <script type="text/javascript" src="https://jss.huatuo6.com/mobile/detail_dy.js"></script> <script type="text/javascript" src="https://jss.huatuo0.cn/mobile/detail_left.js"></script> <script type="text/javascript" src="https://jss.huatuo0.cn/mobile/detail_gg2.js"></script> </div><div on="click:showmore01.toggle" data-closetext="收起全文" class="mip-showmore-btn">显示全文</div> </div> <script type="text/javascript" src="https://jss.huatuo0.cn/mobile/detail_foot.js"></script> <script type="text/javascript" src="https://jss.huatuo0.cn/mobile/share_cebian_gg1.js"></script> <script type="text/javascript" src="https://jss.huatuo6.com/mobile/share_cebian_xgyd.js"></script> <script type="text/javascript" src="https://jss.huatuo0.cn/mobile/share_cebian_gg2.js"></script> <script type="text/javascript" src="https://jss.huatuo6.com/mobile/share_cebian_rmtj.js"></script> <footer class="footer"> <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> </footer> <script> var swiper = new Swiper('.ss .swiper-container', { pagination: '.ss .swiper-pagination', slidesPerView: 3, slidesPerColumn: 2, paginationClickable: true, spaceBetween: 30 }); </script> <script> var swiper = new Swiper('.tech_banner .swiper-container', { pagination: '.tech_banner .swiper-pagination', paginationClickable: true, spaceBetween: 30, centeredSlides: true, autoplay: 2500, autoplayDisableOnInteraction: false, loop: true, }); </script> <script> var swiper = new Swiper('.indextop_hot_t .swiper-container', { pagination: '.indextop_hot_t .swiper-pagination', direction: 'vertical', slidesPerView: 1, paginationClickable: true, spaceBetween: 30, mousewheelControl: true, autoplay : 2000, loop: true, }); </script> <script> var swiper = new Swiper('.scroll_pic .swiper-container', { pagination: '.travel_guide .swiper-pagination', slidesPerView: 'auto', paginationClickable: true, centeredSlides: false, spaceBetween: 0, freeMode: true, }); </script> <!--弹窗开始--> <script type="text/javascript"> $(function() { //弹窗2 $('.menu_icon').click(function() { $('.tbox').show(); }) //关闭 $('.closebtn').click(function() { $('.tbox').hide(); }) }) </script> <!--弹窗结束--> <script type="text/javascript" src="https://jss.huatuo0.cn/mobile/foot_foot.js"></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_foot.js"></script> </body> </html>