JSON

JavaScript实现XML与JSON互转代码(2)

字号+ 作者:H5之家 来源:H5之家 2017-08-20 17:03 我要评论( )

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[

 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].push) == "undefined") {
     var old = obj[nodeName];
     obj[nodeName] = [];
     obj[nodeName].push(old);
    }
    obj[nodeName].push(xmlToJson(item));
   }
  }
 }
 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, XML that looks like:

<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 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;
}

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • JSON使用 JSON JSON数据 JSON教程 JSON使用

    JSON使用 JSON JSON数据 JSON教程 JSON使用

    2017-08-19 16:01

  • javascript转义字符(\)对JSON.parse和eval的影响

    javascript转义字符(\)对JSON.parse和eval的影响

    2017-08-18 18:00

  • jQuery调用WebService返回JSON数据

    jQuery调用WebService返回JSON数据

    2017-08-18 11:01

  • passtome的专栏

    passtome的专栏

    2017-08-18 10:04

网友点评
5