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

Unwanted field in xml to json conversion?

本文关键字:不需要 字段 转换 json xml      更新时间:2023-09-26

如果json字符串中的文本值为空,我如何删除它,并且只有当它具有文本节点类型时才显示。"#text":["''n''t''t''t","''n''t''t''t","''n''t''t''t","''n''t''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"}}}]}} 

编辑、更新

尝试

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

jsfiddlehttp://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>