js实现xml与json互转

<!DOCTYPE html>      
<html lang="en">      
<head>      
    <meta charset="UTF-8">      
    <title>js实现xml与json互转</title>    
    <style type="text/css">    
        html,body{width:100%;height:100%;margin:0;}     
    </style>    
</head>      
<body>     
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.js"></script>    
<script type="text/javascript" src="http://www.jsons.cn/Down/jquery.json2xml.js"></script>
<script type="text/javascript" src="http://www.jsons.cn/Down/jquery.xml2json.js"></script>
 
<script type="text/javascript"> 
 
//参考链接:http://www.jsons.cn/articleinfo/8/
 
var json = {
  "alexa": {
    "@attributes": {
      "ver": "0.9",
      "url": "http://coursesweb.net/",
      "home": "0",
      "aid": "="
    },
    "#text": "",
    "sd": [
      {
        "@attributes": {
          "title": "a",
          "host": "coursesweb.net"
        },
        "#text": "",
        "title": {
          "#text": "CoursesWeb: php, mysql, html, css, javascript, ajax, jquery, actionscript, flash"
        },
        "linksin": {
          "@attributes": {
            "num": "1102"
          }
        },
        "speed": {
          "@attributes": {
            "pct": "51"
          },
          "#text": "4578"
        }
      },
      {
        "#text": "",
        "popularity": {
          "#text": "5777"
        },
        "reach": {
          "@attributes": {
            "rank": "5952"
          }
        },
        "rank": {
          "@attributes": {
            "url": "http://coursesweb.net/"
          },
          "#text": "6667"
        }
      }
    ]
  }
};
 
var xml_content = $.json2xml(json);
console.log(xml_content);
 
var xmlstr = `<alexa ver="0.9" url="http://coursesweb.net/" home="0" aid="=">  
  <sd title="a" host="coursesweb.net">  
    <title>CoursesWeb: php, mysql, html, css, javascript, ajax, jquery, actionscript, flash</title>  
    <linksin num="1102"/>  
    <speed pct="51">4578</speed>  
  </sd>  
  <sd>  
    <popularity>5777</popularity>  
    <reach rank="5952"/>  
    <rank url="http://coursesweb.net/">6667</rank>  
  </sd>  
</alexa>`; 
 
var json_obj = $.xml2json(xmlstr);
console.log(json_obj);
 
</script>        
</body>      
</html>

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值