你的位置:首页 > Java教程

[Java教程]JavaScript实现XML与JSON互转代码(转载)


  下面来分享一个关于JavaScript实现

  最近在开发在线

  

  //  <article>  <header id="h1"> 文章标题 </header>  <section id="s1">  <header> 章节标题 </header>  <p> 章节段落 </p>  </section>  </article>  //JSON表现形式  {  "article": {  "header": {  "#text": "文章标题",  "@id": "h1"  },  "section": {  "@id": "s1",  "header": "章节标题",  "p": "章节段落"  }  }  }

  用Js将

  x2js  : https://code.google.com/p/x2js/

  json

  JKL.Parse

  x2js不会将下面的

//<p> <strong>章节</strong>段<em>落</em> </p>

  而第2个脚本json

{"p":"<strong>章节</strong>段<em>落</em>"}}

  之后我做了些改动,将它解析成如下格式后,满足了“文本混合标签”可正确还原的情况。

{"p":[{"strong":"章节"},"段",{"em":"落"}]}

  另外,形如下面的代码,使用上文提到的脚本进行转换,也会导致无法正确还原的情况。

<article>  <section id="s1">第一节</section>  <header id="h1"> 标题 </header>  <section id="s2">第二节</section></article>

  同样,在一个标签内,它的子标签出现了大于一次,如果需要记录数据的路径,应该使用数组来保存这个结构。正确的代码应该是:

 {  "article": [ {  "section": {  "#text": "第一节",  "@id": "s1"  },  }, {  "header": {  "#text": "标题",  "@id": "h1"  }  }, {  "section": {  "#text": "第一节",  "@id": "s2"  }  }  ]  }

  jkl.parse

SAMPLE <?"1.0" encoding="UTF-8" standalone="yes"?><items> <item>  <zip_cd>10036</zip_cd>  <us_state>NY</us_state>  <us_city>New York</us_city>  <us_dist>Broadway</us_dist> </item></items>SAMPLE SCRIPT:<script type="text/javascript" src="jkl-parse"></script><script><!--  var url = "zip-e.";  var new JKL.Parsevar data = "items"]["item"]["us_state"] );  document.write( data.items.item.us_state );// --></script>OUTPUT JSON:{ items: {  item: {   zip_cd: "1000001"   us_state: "NY",   us_city: "New York",   us_dist: "Broadway",  } }};

json

// Changes function // Create the return object var obj = {}; if (1) { // element // do attributes if (0) { obj["@attributes"] = {};  for (var j = 0; j < ) {  var attribute = "@attributes"][attribute.nodeName] = attribute.nodeValue;  } } } else if (3) { // text obj = // do children if (for(var i = 0; i < ) {  var item = var nodeName = item.nodeName;  if (typeof(obj[nodeName]) == "undefined") {  obj[nodeName] = else {  if (typeof(obj[nodeName].push) == "undefined") {   var old = obj[nodeName];   obj[nodeName] = [];   obj[nodeName].push(old);  }  obj[nodeName].push(return obj;};The major change I needed to implement was using attributes.item(j) instead of the attributes[j] that most of the scripts I found used. With this function, <ALEXA VER="0.9" URL="davidwalsh.name/" HOME="0" AID="="> <SD TITLE="A" FLAGS="" HOST="davidwalsh.name"> <TITLE TEXT="David Walsh Blog :: PHP, MySQL, CSS, Javascript, MooTools, and Everything Else"/> <LINKSIN NUM="1102"/> <SPEED TEXT="1421" PCT="51"/> </SD> <SD> <POPULARITY URL="davidwalsh.name/" TEXT="7131"/> <REACH RANK="5952"/> <RANK DELTA="-1648"/> </SD></ALEXA>...becomes workable a JavaScript object with the following structure:{ "@attributes": { AID: "=", HOME: 0, URL: "davidwalsh.name/", VER: "0.9", }, SD = [ {  "@attributes": {  FLAGS: "",  HOST: "davidwalsh.name",  TITLE: A  },  LINKSIN: {  "@attributes": {   NUM: 1102  }  },  SPEED: {  "@attributes": {   PCT: 51,   TEXT: 1421  }  },  TITLE: {  "@attributes": {   TEXT: "David Walsh Blog :: PHP, MySQL, CSS, Javascript, MooTools, and Everything Else",  }  }, }, {  POPULARITY: {  "@attributes": {   TEXT: 7131,   URL: "davidwalsh.name/"  }  },  RANK: {  "@attributes": {   DELTA: "-1648"  }  },  REACH: {  "@attributes": {   RANK = 5952  }  } } ]}

  说了半天下面整理了一个例子

function // Create the return object var obj = {};if (1) { // element // do attributes if (0) { obj["@attributes"] = {}; for (var j = 0; j < ) { var attribute = "@attributes"][attribute.nodeName] = attribute.nodeValue; } } } else if (3) { // text obj = // do children if (for (var i = 0; i < ) { var item = var nodeName = item.nodeName; if (typeof (obj[nodeName]) == "undefined") { obj[nodeName] = else { if (typeof (obj[nodeName].length) == "undefined") { var old = obj[nodeName]; obj[nodeName] = []; obj[nodeName].push(old); } obj[nodeName].push(return obj; };