本文介绍了在XML中JSON的转换不需要的领域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我怎样才能删除JSON字符串文本值,如果它是空的,显示仅当它有文字NODETYPE。#text:\ñ\ t \ T,\ñ\ t \ T,\ñ\ t \ T,\ñ\ t \ T,\ñ\ T],
这可能是该输出的原因,
xmldoc.xml
< ALEXA VER =0.9URL =davidwalsh.name/HOME =0AID ==>
< SD TITLE =A标志=HOST =davidwalsh.name>
<标题文本=大卫·沃尔什博客:: PHP,MySQL和CSS,Javascript代码,MooTools的,和其他一切/>
< LINKSIN NUM =1102/>
<速度TEXT =1421PCT =51/>
< TEXT上限=12222PCT =87/>
< / SD>
< SD>
<普及URL =davidwalsh.name/TEXT =7131/>
< REACH RANK =5952/>
< RANK DELTA = - 1648/>
< TEXT上限=122yastd22PCT =87/>
< / SD>
< / ALEXA>
脚本code中的index.html:
<脚本>
如果(window.XMLHtt prequest)
{// $ C $下IE7 +,火狐,Chrome,歌剧,Safari浏览器
XMLHTTP =新XMLHtt prequest();
}
其他
{// code对IE6,IE5
XMLHTTP =新的ActiveXObject(Microsoft.XMLHTTP);
}
xmlhttp.open(GET,xmldoc.xml,假);
xmlhttp.send();
xmlDoc中= xmlhttp.responseXML;
VAR jsonText = JSON.stringify(xmlToJson(xmlDoc中));
//警报(jsonText);
文件撰写(jsonText);
//改变XML到JSON
功能xmlToJson(XML){
//创建返回的对象
变种物镜= {};
如果(xml.nodeType == 1){//元件
//做属性
如果(xml.attributes.length大于0){
OBJ [@属性] = {};
对于(VAR J = 0; J< xml.attributes.length; J ++){
var属性= xml.attributes.item(J);
OBJ [@属性] [attribute.nodeName] = attribute.nodeValue;
}
}
}否则,如果(xml.nodeType == 3){//文
OBJ = xml.nodeValue;
}
//做儿童
如果(xml.hasChildNodes()){
对于(VAR I = 0; I< xml.childNodes.length;我++){
变种项= xml.childNodes.item(ⅰ);
VAR节点名称= item.nodeName;
如果(typeof运算(OBJ [节点名称])==未定义){
OBJ [节点名称] = xmlToJson(项目);
} 其他 {
如果(typeof运算(OBJ [节点名称] .push)==未定义){
VAR老的obj = [节点名称]。
OBJ [节点名称] = [];
OBJ [节点名称] .push(旧);
}
OBJ [节点名称] .push(xmlToJson(项目));
}
}
}
返回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沃尔什博客:: PHP,MySQL和CSS,Javascript代码,MooTools的,和一切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"}}}]}}
解决方案
编辑,更新
尝试
函数xmlToJson(XML){
//创建返回的对象
变种物镜= {};
如果(xml.nodeType == 1){//元件
//做属性
如果(xml.attributes.length大于0){
OBJ [@属性] = {};
对于(VAR J = 0; J< xml.attributes.length; J ++){
var属性= xml.attributes.item(J);
OBJ [@属性] [attribute.nodeName] = attribute.nodeValue;
}
}
}
//做儿童
如果(xml.hasChildNodes()){
对于(VAR I = 0; I< xml.childNodes.length;我++){
变种项= xml.childNodes.item(ⅰ);
//如果`nodeName` _不_`#text`和`nodeType` _not_ 3,
//定义`nodeName`为'obj`财产
如果(!item.nodeName ==#text与&&安培;!item.nodeType == 3){
VAR节点名称= item.nodeName;
如果(typeof运算(OBJ [节点名称])==未定义){
OBJ [节点名称] = xmlToJson(项目);
xmlToJson(项目)
} 其他 {
如果(typeof运算(OBJ [节点名称] .push)==未定义){
VAR老的obj = [节点名称]。
OBJ [节点名称] = [];
OBJ [节点名称] .push(旧);
}
OBJ [节点名称] .push(xmlToJson(项目));
}
}
}
};
返回OBJ;
};
http://jsfiddle.net/guest271314/9kuhLok8/
VAR XML = document.getElementsByTagName(文本域)[0] .value的;VAR分析器=新的DOMParser();VAR xmlDoc中= parser.parseFromString(XML,为text / xml); //`xml`文件VAR JSON = xmlToJson(xmlDoc中);VAR jsonText = JSON.stringify(JSON,空,2);VAR的elem = document.createElement方法(pre);elem.innerText = jsonText;document.body.insertBefore(ELEM,document.body.firstChild);执行console.log(JSON); //`json`对象功能xmlToJson(XML){ //创建返回的对象 变种物镜= {}; 如果(xml.nodeType == 1){//元件 //做属性 如果(xml.attributes.length大于0){ OBJ [@属性] = {}; 对于(VAR J = 0; J< xml.attributes.length; J ++){ var属性= xml.attributes.item(J); OBJ [@属性] [attribute.nodeName] = attribute.nodeValue; } } } //做儿童 如果(xml.hasChildNodes()){ 对于(VAR I = 0; I< xml.childNodes.length;我++){ 变种项= xml.childNodes.item(ⅰ); //如果`nodeName` _不_`#text`和`nodeType` _not_ 3, //定义`nodeName`为'obj`财产 如果(!item.nodeName ==#text与&&安培;!item.nodeType == 3){ VAR节点名称= item.nodeName; 如果(typeof运算(OBJ [节点名称])==未定义){ OBJ [节点名称] = xmlToJson(项目); xmlToJson(项目) } 其他 { 如果(typeof运算(OBJ [节点名称] .push)==未定义){ VAR老的obj = [节点名称]。 OBJ [节点名称] = []; OBJ [节点名称] .push(旧); } OBJ [节点名称] .push(xmlToJson(项目)); } } } }; 返回OBJ;};
< textarea的风格=宽度:500px的;高度:300像素;> < ALEXA VER =0.9URL =davidwalsh.name/HOME =0AID ==> < SD TITLE =A标志=HOST =davidwalsh.name> <标题文本=大卫·沃尔什博客:: PHP,MySQL和CSS,Javascript代码,MooTools的,和其他一切/> < LINKSIN NUM =1102/> <速度TEXT =1421PCT =51/> < TEXT上限=12222PCT =87/> < / SD> < SD> <普及URL =davidwalsh.name/TEXT =7131/> < REACH RANK =5952/> < RANK DELTA = - 1648/> < TEXT上限=122yastd22PCT =87/> < / SD> < / ALEXA>< / textarea的>
How can I remove the text value in the json string if it is empty and show up only if it has text nodetype."#text":["\n\t\t","\n\t\t","\n\t\t","\n\t\t","\n\t"],
What may be the reason for this output,
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>
script code in 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>
output in 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"}}}]}}
解决方案
Edit, Updated
Try
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>
这篇关于在XML中JSON的转换不需要的领域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!