1
- // https://stackoverflow.com/a/1773571
2
-
1
+ // https://stackoverflow.com/a/1773571/11898496
3
2
export function parseXml ( xml ) {
4
3
var dom = null ;
5
4
if ( window . DOMParser ) {
@@ -23,196 +22,4 @@ export function parseXml(xml) {
23
22
return dom ;
24
23
}
25
24
26
- export function xml2json ( xml , tab ) {
27
- var X = {
28
- toObj : function ( xml ) {
29
- var o = { } ;
30
- if ( xml . nodeType == 1 ) {
31
- // element node ..
32
- if ( xml . attributes . length )
33
- // element with attributes ..
34
- for ( var i = 0 ; i < xml . attributes . length ; i ++ )
35
- o [ "@" + xml . attributes [ i ] . nodeName ] = (
36
- xml . attributes [ i ] . nodeValue || ""
37
- ) . toString ( ) ;
38
- if ( xml . firstChild ) {
39
- // element has child nodes ..
40
- var textChild = 0 ,
41
- cdataChild = 0 ,
42
- hasElementChild = false ;
43
- for ( var n = xml . firstChild ; n ; n = n . nextSibling ) {
44
- if ( n . nodeType == 1 ) hasElementChild = true ;
45
- else if ( n . nodeType == 3 && n . nodeValue . match ( / [ ^ \f \n \r \t \v ] / ) )
46
- textChild ++ ; // non-whitespace text
47
- else if ( n . nodeType == 4 ) cdataChild ++ ; // cdata section node
48
- }
49
- if ( hasElementChild ) {
50
- if ( textChild < 2 && cdataChild < 2 ) {
51
- // structured element with evtl. a single text or/and cdata node ..
52
- X . removeWhite ( xml ) ;
53
- for ( var n = xml . firstChild ; n ; n = n . nextSibling ) {
54
- if ( n . nodeType == 3 )
55
- // text node
56
- o [ "#text" ] = X . escape ( n . nodeValue ) ;
57
- else if ( n . nodeType == 4 )
58
- // cdata node
59
- o [ "#cdata" ] = X . escape ( n . nodeValue ) ;
60
- else if ( o [ n . nodeName ] ) {
61
- // multiple occurence of element ..
62
- if ( o [ n . nodeName ] instanceof Array )
63
- o [ n . nodeName ] [ o [ n . nodeName ] . length ] = X . toObj ( n ) ;
64
- else o [ n . nodeName ] = [ o [ n . nodeName ] , X . toObj ( n ) ] ;
65
- } // first occurence of element..
66
- else o [ n . nodeName ] = X . toObj ( n ) ;
67
- }
68
- } else {
69
- // mixed content
70
- if ( ! xml . attributes . length ) o = X . escape ( X . innerXml ( xml ) ) ;
71
- else o [ "#text" ] = X . escape ( X . innerXml ( xml ) ) ;
72
- }
73
- } else if ( textChild ) {
74
- // pure text
75
- if ( ! xml . attributes . length ) o = X . escape ( X . innerXml ( xml ) ) ;
76
- else o [ "#text" ] = X . escape ( X . innerXml ( xml ) ) ;
77
- } else if ( cdataChild ) {
78
- // cdata
79
- if ( cdataChild > 1 ) o = X . escape ( X . innerXml ( xml ) ) ;
80
- else
81
- for ( var n = xml . firstChild ; n ; n = n . nextSibling )
82
- o [ "#cdata" ] = X . escape ( n . nodeValue ) ;
83
- }
84
- }
85
- if ( ! xml . attributes . length && ! xml . firstChild ) o = null ;
86
- } else if ( xml . nodeType == 9 ) {
87
- // document.node
88
- o = X . toObj ( xml . documentElement ) ;
89
- } else alert ( "unhandled node type: " + xml . nodeType ) ;
90
- return o ;
91
- } ,
92
- toJson : function ( o , name , ind ) {
93
- var json = name ? '"' + name + '"' : "" ;
94
- if ( o instanceof Array ) {
95
- for ( var i = 0 , n = o . length ; i < n ; i ++ )
96
- o [ i ] = X . toJson ( o [ i ] , "" , ind + "\t" ) ;
97
- json +=
98
- ( name ? ":[" : "[" ) +
99
- ( o . length > 1
100
- ? "\n" + ind + "\t" + o . join ( ",\n" + ind + "\t" ) + "\n" + ind
101
- : o . join ( "" ) ) +
102
- "]" ;
103
- } else if ( o == null ) json += ( name && ":" ) + "null" ;
104
- else if ( typeof o == "object" ) {
105
- var arr = [ ] ;
106
- for ( var m in o ) arr [ arr . length ] = X . toJson ( o [ m ] , m , ind + "\t" ) ;
107
- json +=
108
- ( name ? ":{" : "{" ) +
109
- ( arr . length > 1
110
- ? "\n" + ind + "\t" + arr . join ( ",\n" + ind + "\t" ) + "\n" + ind
111
- : arr . join ( "" ) ) +
112
- "}" ;
113
- } else if ( typeof o == "string" )
114
- json += ( name && ":" ) + '"' + o . toString ( ) + '"' ;
115
- else json += ( name && ":" ) + o . toString ( ) ;
116
- return json ;
117
- } ,
118
- innerXml : function ( node ) {
119
- var s = "" ;
120
- if ( "innerHTML" in node ) s = node . innerHTML ;
121
- else {
122
- var asXml = function ( n ) {
123
- var s = "" ;
124
- if ( n . nodeType == 1 ) {
125
- s += "<" + n . nodeName ;
126
- for ( var i = 0 ; i < n . attributes . length ; i ++ )
127
- s +=
128
- " " +
129
- n . attributes [ i ] . nodeName +
130
- '="' +
131
- ( n . attributes [ i ] . nodeValue || "" ) . toString ( ) +
132
- '"' ;
133
- if ( n . firstChild ) {
134
- s += ">" ;
135
- for ( var c = n . firstChild ; c ; c = c . nextSibling ) s += asXml ( c ) ;
136
- s += "</" + n . nodeName + ">" ;
137
- } else s += "/>" ;
138
- } else if ( n . nodeType == 3 ) s += n . nodeValue ;
139
- else if ( n . nodeType == 4 ) s += "<![CDATA[" + n . nodeValue + "]]>" ;
140
- return s ;
141
- } ;
142
- for ( var c = node . firstChild ; c ; c = c . nextSibling ) s += asXml ( c ) ;
143
- }
144
- return s ;
145
- } ,
146
- escape : function ( txt ) {
147
- return txt
148
- . replace ( / [ \\ ] / g, "\\\\" )
149
- . replace ( / [ \" ] / g, '\\"' )
150
- . replace ( / [ \n ] / g, "\\n" )
151
- . replace ( / [ \r ] / g, "\\r" ) ;
152
- } ,
153
- removeWhite : function ( e ) {
154
- e . normalize ( ) ;
155
- for ( var n = e . firstChild ; n ; ) {
156
- if ( n . nodeType == 3 ) {
157
- // text node
158
- if ( ! n . nodeValue . match ( / [ ^ \f \n \r \t \v ] / ) ) {
159
- // pure whitespace text node
160
- var nxt = n . nextSibling ;
161
- e . removeChild ( n ) ;
162
- n = nxt ;
163
- } else n = n . nextSibling ;
164
- } else if ( n . nodeType == 1 ) {
165
- // element node
166
- X . removeWhite ( n ) ;
167
- n = n . nextSibling ;
168
- } // any other node
169
- else n = n . nextSibling ;
170
- }
171
- return e ;
172
- } ,
173
- } ;
174
- if ( xml . nodeType == 9 )
175
- // document node
176
- xml = xml . documentElement ;
177
- var json = X . toJson ( X . toObj ( X . removeWhite ( xml ) ) , xml . nodeName , "\t" ) ;
178
- return (
179
- "{\n" +
180
- tab +
181
- ( tab ? json . replace ( / \t / g, tab ) : json . replace ( / \t | \n / g, "" ) ) +
182
- "\n}"
183
- ) ;
184
- }
185
-
186
- export function json2xml ( o , tab ) {
187
- var toXml = function ( v , name , ind ) {
188
- var xml = "" ;
189
- if ( v instanceof Array ) {
190
- for ( var i = 0 , n = v . length ; i < n ; i ++ )
191
- xml += ind + toXml ( v [ i ] , name , ind + "\t" ) + "\n" ;
192
- } else if ( typeof v == "object" ) {
193
- var hasChild = false ;
194
- xml += ind + "<" + name ;
195
- for ( var m in v ) {
196
- if ( m . charAt ( 0 ) == "@" )
197
- xml += " " + m . substr ( 1 ) + '="' + v [ m ] . toString ( ) + '"' ;
198
- else hasChild = true ;
199
- }
200
- xml += hasChild ? ">" : "/>" ;
201
- if ( hasChild ) {
202
- for ( var m in v ) {
203
- if ( m == "#text" ) xml += v [ m ] ;
204
- else if ( m == "#cdata" ) xml += "<![CDATA[" + v [ m ] + "]]>" ;
205
- else if ( m . charAt ( 0 ) != "@" ) xml += toXml ( v [ m ] , m , ind + "\t" ) ;
206
- }
207
- xml +=
208
- ( xml . charAt ( xml . length - 1 ) == "\n" ? ind : "" ) + "</" + name + ">" ;
209
- }
210
- } else {
211
- xml += ind + "<" + name + ">" + v . toString ( ) + "</" + name + ">" ;
212
- }
213
- return xml ;
214
- } ,
215
- xml = "" ;
216
- for ( var m in o ) xml += toXml ( o [ m ] , m , "" ) ;
217
- return tab ? xml . replace ( / \t / g, tab ) : xml . replace ( / \t | \n / g, "" ) ;
218
- }
25
+ // https://stackoverflow.com/a/20861541/11898496
0 commit comments