今天写的一段代码,用于将XML的对象/字符串转化成JSON的对象/字符串。
网上有一个这样的代码的,不过那个拼字符串拼出来……满屏的正则表达式,于是也没有细看……而且那个是LGPL的License,用起来还是有顾忌的。
随便写了写。这东西本来就应该封个包静态调用或者弄成单例模式。不过反正现在这样也能用,就无所谓了。
用法:var json = new Xml2Json(xmlObject).parse(containRoot, returnText);
xmlObject可以为xml的对象或者是字符串,containRoot用来表示生成的json中要不要包含根元素,而returnText表示是以文本还是以对象形式返回json。这两个属性默认均为false。
1
var
Xml2Json
=
Class.create();
2![](/Images/OutliningIndicators/None.gif)
3![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
Xml2Json.prototype
=
{
4![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
initialize : function(xmlElem)
{
5![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (typeof(xmlElem) === 'object')
{
6
this.doc = xmlElem;
7![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} else if (typeof(xmlElem) === 'string')
{
8![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (window.ActiveXObject)
{
9
this.doc = new ActiveXObject('Microsoft.XMLDOM');
10
this.doc.async = 'false';
11
this.doc.loadXML(xmlElem);
12![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} else
{
13
this.doc = new DOMParser().parseFromString(xmlElem, 'text/xml');
14
}
15
}
16
},
17![](/Images/OutliningIndicators/InBlock.gif)
18![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
parse : function(containRoot, returnText)
{
19
var root = this.doc.documentElement || this.doc;
20![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
var json = this.__parse__(root,
{});
21![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
var returnValue =
{};
22
if (containRoot)
23
returnValue[root.nodeName] = json;
24
else
25
returnValue = json;
26
return returnText ? this.convert(returnValue) : returnValue;
27
},
28![](/Images/OutliningIndicators/InBlock.gif)
29![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
convert : function(jsonObject)
{
30
return this.__convert__(jsonObject, 0);
31
},
32![](/Images/OutliningIndicators/InBlock.gif)
33![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
__blank__ : function(s)
{
34
var pattern = /^[\s]*$/g;
35
return pattern.test(s);
36
},
37![](/Images/OutliningIndicators/InBlock.gif)
38![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
__convert__ : function(jsonObject, level)
{
39
var template;
40
var i;
41
var j;
42![](/Images/OutliningIndicators/InBlock.gif)
43
var tabs = '';
44![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for (i = 0;i < level; i++)
{
45
tabs += '\t';
46
}
47![](/Images/OutliningIndicators/InBlock.gif)
48![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (jsonObject instanceof Array)
{
49
template = '[\n';
50![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for (i = 0;i < jsonObject.length - 1; i++)
{
51
template += tabs + '\t'
52
+ this.__convert__(jsonObject[i], level + 1) + ',\n';
53
}
54
template += tabs
55
+ '\t'
56
+ this.__convert__(jsonObject[jsonObject.length - 1], level
57
+ 1) + '\n' + tabs + ']';
58
return template;
59![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} else if (typeof(jsonObject) === 'object')
{
60![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
template = '
{';
61![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for (var property in jsonObject)
{
62
template += '\n\t' + tabs + property + ': '
63
+ this.__convert__(jsonObject[property], level + 1)
64
+ ',';
65
}
66
template = template.substring(0, template.length - 1);
67
template += '\n' + tabs + '}';
68
return template;
69![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} else if (typeof(jsonObject) === 'string')
{
70
return '"' + jsonObject + '"';
71![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} else
{
72
return jsonObject;
73
}
74
},
75![](/Images/OutliningIndicators/InBlock.gif)
76![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
__parse__ : function(elem, obj)
{
77![](/Images/OutliningIndicators/InBlock.gif)
78![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for (var i = 0;i < elem.attributes.length; i++)
{
79
var attr = elem.attributes[i];
80
obj[attr.name] = attr.value;
81
}
82![](/Images/OutliningIndicators/InBlock.gif)
83![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for (i = 0;i < elem.childNodes.length; i++)
{
84
var child = elem.childNodes[i];
85
var property = child.nodeName;
86
if (child.nodeType !== Node.TEXT_NODE
87![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
&& child.nodeType !== Node.COMMENT_NODE)
{
88![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
var childObj = this.__parse__(child,
{});
89
if (!obj[property])
90
obj[property] = [];
91
obj[property].push(childObj);
92
} else if (child.nodeType === Node.TEXT_NODE
93![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
&& !this.__blank__(child.data))
{
94
obj['value'] = child.data;
95
}
96
}
97![](/Images/OutliningIndicators/InBlock.gif)
98
return this.__reduce__(obj);
99
},
100![](/Images/OutliningIndicators/InBlock.gif)
101![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
__reduce__ : function(obj)
{
102
var count = 0;
103![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for (var property in obj)
{
104
var value = obj[property];
105![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (value instanceof Array && value.length === 1)
{
106
obj[property] = value[0];
107
}
108
count++;
109
}
110
if (count === 1 && obj['value'])
111
obj = obj['value'];
112
return obj;
113
}
114
}
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/None.gif)
3
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
4
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
5
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
6
![](/Images/OutliningIndicators/InBlock.gif)
7
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
8
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
9
![](/Images/OutliningIndicators/InBlock.gif)
10
![](/Images/OutliningIndicators/InBlock.gif)
11
![](/Images/OutliningIndicators/InBlock.gif)
12
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
13
![](/Images/OutliningIndicators/InBlock.gif)
14
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
15
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
16
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
17
![](/Images/OutliningIndicators/InBlock.gif)
18
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
19
![](/Images/OutliningIndicators/InBlock.gif)
20
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
21
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
22
![](/Images/OutliningIndicators/InBlock.gif)
23
![](/Images/OutliningIndicators/InBlock.gif)
24
![](/Images/OutliningIndicators/InBlock.gif)
25
![](/Images/OutliningIndicators/InBlock.gif)
26
![](/Images/OutliningIndicators/InBlock.gif)
27
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
28
![](/Images/OutliningIndicators/InBlock.gif)
29
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
30
![](/Images/OutliningIndicators/InBlock.gif)
31
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
32
![](/Images/OutliningIndicators/InBlock.gif)
33
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
34
![](/Images/OutliningIndicators/InBlock.gif)
35
![](/Images/OutliningIndicators/InBlock.gif)
36
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
37
![](/Images/OutliningIndicators/InBlock.gif)
38
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
39
![](/Images/OutliningIndicators/InBlock.gif)
40
![](/Images/OutliningIndicators/InBlock.gif)
41
![](/Images/OutliningIndicators/InBlock.gif)
42
![](/Images/OutliningIndicators/InBlock.gif)
43
![](/Images/OutliningIndicators/InBlock.gif)
44
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
45
![](/Images/OutliningIndicators/InBlock.gif)
46
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
47
![](/Images/OutliningIndicators/InBlock.gif)
48
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
49
![](/Images/OutliningIndicators/InBlock.gif)
50
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
51
![](/Images/OutliningIndicators/InBlock.gif)
52
![](/Images/OutliningIndicators/InBlock.gif)
53
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
54
![](/Images/OutliningIndicators/InBlock.gif)
55
![](/Images/OutliningIndicators/InBlock.gif)
56
![](/Images/OutliningIndicators/InBlock.gif)
57
![](/Images/OutliningIndicators/InBlock.gif)
58
![](/Images/OutliningIndicators/InBlock.gif)
59
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
60
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
61
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
62
![](/Images/OutliningIndicators/InBlock.gif)
63
![](/Images/OutliningIndicators/InBlock.gif)
64
![](/Images/OutliningIndicators/InBlock.gif)
65
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
66
![](/Images/OutliningIndicators/InBlock.gif)
67
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
68
![](/Images/OutliningIndicators/InBlock.gif)
69
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
70
![](/Images/OutliningIndicators/InBlock.gif)
71
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
72
![](/Images/OutliningIndicators/InBlock.gif)
73
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
74
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
75
![](/Images/OutliningIndicators/InBlock.gif)
76
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
77
![](/Images/OutliningIndicators/InBlock.gif)
78
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
79
![](/Images/OutliningIndicators/InBlock.gif)
80
![](/Images/OutliningIndicators/InBlock.gif)
81
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
82
![](/Images/OutliningIndicators/InBlock.gif)
83
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
84
![](/Images/OutliningIndicators/InBlock.gif)
85
![](/Images/OutliningIndicators/InBlock.gif)
86
![](/Images/OutliningIndicators/InBlock.gif)
87
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
88
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
89
![](/Images/OutliningIndicators/InBlock.gif)
90
![](/Images/OutliningIndicators/InBlock.gif)
91
![](/Images/OutliningIndicators/InBlock.gif)
92
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
93
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
94
![](/Images/OutliningIndicators/InBlock.gif)
95
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
96
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
97
![](/Images/OutliningIndicators/InBlock.gif)
98
![](/Images/OutliningIndicators/InBlock.gif)
99
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
100
![](/Images/OutliningIndicators/InBlock.gif)
101
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
102
![](/Images/OutliningIndicators/InBlock.gif)
103
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
104
![](/Images/OutliningIndicators/InBlock.gif)
105
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
106
![](/Images/OutliningIndicators/InBlock.gif)
107
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
108
![](/Images/OutliningIndicators/InBlock.gif)
109
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
110
![](/Images/OutliningIndicators/InBlock.gif)
111
![](/Images/OutliningIndicators/InBlock.gif)
112
![](/Images/OutliningIndicators/InBlock.gif)
113
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
114
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)