xml到json转换中的不需要的字段?

时间:2014-10-20 18:52:02

标签: javascript html ajax xml json

如果json字符串为空,我如何删除它中的文本值,只有当它具有文本nodetype时才会显示。 “#text”:[ “\ n \吨\ t” 的, “\ n \吨\ t” 的, “\ n \吨\ t” 的, “\ n \吨\ t” 的, “\ n \ t” 的],

这个输出可能是什么原因,

xmldoc.xml

<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"/>
        <limit TEXT="12222" PCT="87"/>
    </SD>
    <SD>
        <POPULARITY URL="davidwalsh.name/" TEXT="7131"/>
        <REACH RANK="5952"/>
        <RANK DELTA="-1648"/>
        <limit TEXT="122yastd22" PCT="87"/>
    </SD>
</ALEXA>
index.html中的

脚本代码:

<script>

if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  xmlhttp.open("GET","xmldoc.xml",false);
  xmlhttp.send();
  xmlDoc=xmlhttp.responseXML; 

var jsonText = JSON.stringify(xmlToJson(xmlDoc));

//alert(jsonText);

document.write(jsonText);

// 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].push) == "undefined") {
          var old = obj[nodeName];
          obj[nodeName] = [];
          obj[nodeName].push(old);
        }
        obj[nodeName].push(xmlToJson(item));
      }
    }
  }
  return obj;
};
</script>

输出json:

 {"ALEXA":{"@attributes":{"VER":"0.9","URL":"davidwalsh.name/","HOME":"0","AID":"="},"#text":["\n\t","\n\t","\n"],"SD":[{"@attributes":{"TITLE":"A","FLAGS":"","HOST":"davidwalsh.name"},"#text":["\n\t\t","\n\t\t","\n\t\t","\n\t\t","\n\t"],"TITLE":{"@attributes":{"TEXT":"David Walsh Blog :: PHP, MySQL, CSS, Javascript, MooTools, and Everything Else"}},"LINKSIN":{"@attributes":{"NUM":"1102"}},"SPEED":{"@attributes":{"TEXT":"1421","PCT":"51"}},"limit":{"@attributes":{"TEXT":"12222","PCT":"87"}}},{"#text":["\n\t\t","\n\t\t","\n\t\t","\n\t\t","\n\t"],"POPULARITY":{"@attributes":{"URL":"davidwalsh.name/","TEXT":"7131"}},"REACH":{"@attributes":{"RANK":"5952"}},"RANK":{"@attributes":{"DELTA":"-1648"}},"limit":{"@attributes":{"TEXT":"122yastd22","PCT":"87"}}}]}} 

1 个答案:

答案 0 :(得分:0)

修改,更新

尝试

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;
                }
            }
        } 
        // do children
        if (xml.hasChildNodes()) {
            for (var i = 0; i < xml.childNodes.length; i++) {
                var item = xml.childNodes.item(i);
                // if `nodeName` _not_ `#text` and `nodeType` _not_ 3 ,
                // define `nodeName` as property of `obj`
                if (item.nodeName !== "#text" && item.nodeType !== 3) {
                    var nodeName = item.nodeName;
                    if (typeof (obj[nodeName]) == "undefined") {
                        obj[nodeName] = xmlToJson(item);
                        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;
};

jsfiddle http://jsfiddle.net/guest271314/9kuhLok8/

var xml = document.getElementsByTagName("textarea")[0].value; 
var parser = new DOMParser();
var xmlDoc = parser.parseFromString(xml, "text/xml"); // `xml` document

var json = xmlToJson(xmlDoc);
var jsonText = JSON.stringify(json, null, 2);

var elem = document.createElement("pre");
elem.innerText = jsonText;
document.body.insertBefore(elem, document.body.firstChild);
console.log(json); // `json` object

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;
                }
            }
        } 
        // do children
        if (xml.hasChildNodes()) {
            for (var i = 0; i < xml.childNodes.length; i++) {
                var item = xml.childNodes.item(i);
                // if `nodeName` _not_ `#text` and `nodeType` _not_ 3 ,
                // define `nodeName` as property of `obj`
                if (item.nodeName !== "#text" && item.nodeType !== 3) {
                    var nodeName = item.nodeName;
                    if (typeof (obj[nodeName]) == "undefined") {
                        obj[nodeName] = xmlToJson(item);
                        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;
};
<textarea style="width:500px;height:300px;">
    <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" />
            <limit TEXT="12222" PCT="87" />
        </SD>
        <SD>
            <POPULARITY URL="davidwalsh.name/" TEXT="7131" />
            <REACH RANK="5952" />
            <RANK DELTA="-1648" />
            <limit TEXT="122yastd22" PCT="87" />
        </SD>
    </ALEXA>
</textarea>