333
schangxiang@126.com
2025-09-19 18966e02fb573c7e2bb0c6426ed792b38b910940
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
jstoxml
=========
[![Build Status](https://travis-ci.org/davidcalhoun/jstoxml.svg?branch=master)](https://travis-ci.org/davidcalhoun/jstoxml)
 
### Convert JavaScript objects (and JSON) to XML (for RSS, Podcasts, etc.)
 
Everyone loves JSON, and more and more folks want to move that direction, but we still need things outputted in XML!  Particularly for [RSS feeds](http://www.rssboard.org/rss-specification) and [Podcasts](http://www.apple.com/itunes/podcasts/specs.html).
 
This is inspired by [node-jsontoxml](https://github.com/soldair/node-jsontoxml), which was found to be a bit too rough around the edges.  jstoxml attempts to fix that by being more flexible.
 
### Features
* supports a variety of inputs: objects, arrays, strings, 
* tabbed output (optional)
* custom filters (```&``` -> ```&```, etc) (optional)
 
### Installation
* npm install jstoxml
 
### Version 0.2.1
* IMPORTANT: empty text strings will now output as empty XML tags (NOT text content), which makes more sense and is more intuitive (see issue #3).  To output text content, set the value to null instead (see Example 5 below).
 
For instance:
```
jstoxml.toXML({
  a: '1',
  foo: '',
  b: '2'
});
// Output: <a>1</a><foo></foo><b>2</b>
```
 
```
jstoxml.toXML({
  a: '1',
  foo: null,
  b: '2'
});
// Output: <a>1</a>foo<b>2</b>
```
 
### Version 0.1.0
* Added support for custom filters (for XML, UTF-8 entities, or whatever you need it for)
* Changed to a single options object, passed as the second parameter.  This will break older versions that use the XML header or indentation!  They will need to be updated (see the examples below).
 
 
### Examples
First you'll want to require jstoxml in your script, and assign the result to the namespace variable you want to use (in this case jstoxml):
 
```javascript
var jstoxml = require('jstoxml');
```
 
#### Interface
jstoxml has a very simple interface: jstoxml.toXML(input, addHeader [Boolean], indent [String]);
 
* input: accepts objects, arrays, strings, and even functions
* addHeader (optional): pass in true to include the XML header (&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;)
* indent (optional): string which is used as an indent (i.e. '  ')
 
 
#### Example 1: Simple object
```javascript
jstoxml.toXML({
  foo: 'bar',
  foo2: 'bar2'
});
```
Output:
 
```
<foo>bar</foo><foo2>bar2</foo2>
```
 
 
#### Example 2: Simple array
```javascript
jstoxml.toXML([
  {foo: 'bar'},
  {foo2: 'bar2'}
]);
```
Output:
 
```
<foo>bar</foo><foo2>bar2</foo2>
```
 
 
#### Example 3: Duplicate tag names
Because we can't have duplicate keys in objects, we have to take advantage of arrays to get duplicate tag names:
 
```javascript
jstoxml.toXML([
  {foo: 'bar'},
  {foo: 'bar2'}
]);
```
Output:
 
```
<foo>bar</foo><foo>bar2</foo>
```
 
 
#### Example 4: Attributes
```javascript
jstoxml.toXML({
  _name: 'foo',
  _content: 'bar',
  _attrs: {
    a: 'b',
    c: 'd'
  }
});
```
Output:
 
```
<foo a="b" c="d">bar</foo>
```
 
 
#### Example 5: Tags mixed with text content
To output text content, set a key to null:
 
```javascript
jstoxml.toXML({
  'text1': null,
  foo: 'bar',
  'text2': null
});
 
```
Output:
 
```
text1<foo>bar</foo>text2
```
 
 
#### Example 6: Nested tags (with indenting)
 
```javascript
jstoxml.toXML({
  a: {
    foo: 'bar',
    foo2: 'bar2'
  }
}, {header: false, indent: '  '});
```
Output:
 
```
<a>
  <foo>bar</foo>
  <foo2>bar2</foo2>
</a>
```
 
 
#### Example 7: Nested tags with attributes (with indenting)
```javascript
jstoxml.toXML({
  ooo: {
    _name: 'foo',
    _attrs: {
      a: 'b'
    },
    _content: {
      _name: 'bar',
      _attrs: {
        c: 'd'
      }
    }
  }
}, {header: false, indent: '  '});
```
Output:
 
```
<ooo>
  <foo a="b">
    <bar c="d"/>
  </foo>
</ooo>
```
 
Note that cases like this might be especially hard to read because of the deep nesting, so it's recommend you use something like this pattern instead, which breaks it up into more readable pieces:
 
```javascript
var bar = {
  _name: 'bar',
  _attrs: {
    c: 'd'
  }
};
 
var foo = {
  _name: 'foo',
  _attrs: {
    a: 'b'
  },
  _content: bar
}
 
return jstoxml.toXML({
  ooo: foo
}, {header: false, indent: '  '})
```
 
 
#### Example 8: Functions
```javascript
jstoxml.toXML({
  onePlusTwo: function(){
    return 1 + 2;
  },
  date: function(){
    return new Date();
  }
});
```
Output:
 
```
<onePlusTwo>3</onePlusTwo><date>Sat Jul 30 2011 17:49:52 GMT+0900 (JST)</date>
```
 
#### Example 9: RSS Feed
```javascript
jstoxml.toXML({
  _name: 'rss',
  _attrs: {
    version: '2.0'
  },
  _content: {
    channel: [
      {title: 'RSS Example'},
      {description: 'Description'},
      {link: 'google.com'},
      {lastBuildDate: function(){
        return new Date();
      }},
      {pubDate: function(){
        return new Date();
      }},
      {language: 'en'},
      {item: {
        title: 'Item title',
        link: 'Item link',
        description: 'Item Description',
        pubDate: function(){
          return new Date();
        }
      }},
      {item: {
        title: 'Item2 title',
        link: 'Item2 link',
        description: 'Item2 Description',
        pubDate: function(){
          return new Date();
        }
      }}
    ]
  }
}, {header: true, indent: '  '});
```
Output:
 
```
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <title>RSS Example</title>
    <description>Description</description>
    <link>google.com</link>
    <lastBuildDate>Sat Jul 30 2011 18:14:25 GMT+0900 (JST)</lastBuildDate>
    <pubDate>Sat Jul 30 2011 18:14:25 GMT+0900 (JST)</pubDate>
    <language>en</language>
    <item>
      <title>Item title</title>
      <link>Item link</link>
      <description>Item Description</description>
      <pubDate>Sat Jul 30 2011 18:33:47 GMT+0900 (JST)</pubDate>
    </item>
    <item>
      <title>Item2 title</title>
      <link>Item2 link</link>
      <description>Item2 Description</description>
      <pubDate>Sat Jul 30 2011 18:33:47 GMT+0900 (JST)</pubDate>
    </item>
  </channel>
</rss>
```
 
 
#### Example 10: Podcast RSS Feed
(see the [Apple docs](http://www.apple.com/itunes/podcasts/specs.html) for more information)
 
```javascript
jstoxml.toXML({
  _name: 'rss',
  _attrs: {
    'xmlns:itunes': 'http://www.itunes.com/dtds/podcast-1.0.dtd',
    version: '2.0'
  },
  _content: {
    channel: [
      {title: 'Title'},
      {link: 'google.com'},
      {language: 'en-us'},
      {copyright: 'Copyright 2011'},
      {'itunes:subtitle': 'Subtitle'},
      {'itunes:author': 'Author'},
      {'itunes:summary': 'Summary'},
      {description: 'Description'},
      {'itunes:owner': {
        'itunes:name': 'Name',
        'itunes:email': 'Email'
      }},
      {
        _name: 'itunes:image',
        _attrs: {
          href: 'image.jpg'
        }
      },
      {
        _name: 'itunes:category',
        _attrs: {
          text: 'Technology'
        },
        _content: {
          _name: 'itunes:category',
          _attrs: {
            text: 'Gadgets'
          } 
        }
      },
      {
        _name: 'itunes:category',
        _attrs: {
          text: 'TV &amp; Film'
        }
      },
      {
        item: [
          {title: 'Podcast Title'},
          {'itunes:author': 'Author'},
          {'itunes:subtitle': 'Subtitle'},
          {'itunes:summary': 'Summary'},
          {'itunes:image': 'image.jpg'},
          {
            _name: 'enclosure',
            _attrs: {
              url: 'http://example.com/podcast.m4a',
              length: '8727310',
              type: 'audio/x-m4a'
            }
          },
          {guid: 'http://example.com/archive/aae20050615.m4a'},
          {pubDate: 'Wed, 15 Jun 2011 19:00:00 GMT'},
          {'itunes:duration': '7:04'},
          {'itunes:keywords': 'salt, pepper, shaker, exciting'}
        ]
      },
      {
        item: [
          {title: 'Podcast2 Title'},
          {'itunes:author': 'Author2'},
          {'itunes:subtitle': 'Subtitle2'},
          {'itunes:summary': 'Summary2'},
          {'itunes:image': 'image2.jpg'},
          {
            _name: 'enclosure',
            _attrs: {
              url: 'http://example.com/podcast2.m4a',
              length: '655555',
              type: 'audio/x-m4a'
            }
          },
          {guid: 'http://example.com/archive/aae2.m4a'},
          {pubDate: 'Wed, 15 Jul 2011 19:00:00 GMT'},
          {'itunes:duration': '11:20'},
          {'itunes:keywords': 'foo, bar'}
        ]
      }
    ]
  }
}, {header: true, indent: '  '});
```
 
Output:
 
```
<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0">
  <channel>
    <title>Title</title>
    <link>google.com</link>
    <language>en-us</language>
    <copyright>Copyright 2011</copyright>
    <itunes:subtitle>Subtitle</itunes:subtitle>
    <itunes:author>Author</itunes:author>
    <itunes:summary>Summary</itunes:summary>
    <description>Description</description>
    <itunes:owner>
      <itunes:name>Name</itunes:name>
      <itunes:email>Email</itunes:email>
    </itunes:owner>
    <itunes:image href="image.jpg"/>
    <itunes:category text="Technology">
      <itunes:category text="Gadgets"/>
    </itunes:category>
    <itunes:category text="TV &amp; Film"/>
    <item>
      <title>Podcast Title</title>
      <itunes:author>Author</itunes:author>
      <itunes:subtitle>Subtitle</itunes:subtitle>
      <itunes:summary>Summary</itunes:summary>
      <itunes:image>image.jpg</itunes:image>
      <enclosure url="http://example.com/podcast.m4a" length="8727310" type="audio/x-m4a"/>
      <guid>http://example.com/archive/aae20050615.m4a</guid>
      <pubDate>Wed, 15 Jun 2011 19:00:00 GMT</pubDate>
      <itunes:duration>7:04</itunes:duration>
      <itunes:keywords>salt, pepper, shaker, exciting</itunes:keywords>
    </item>
    <item>
      <title>Podcast2 Title</title>
      <itunes:author>Author2</itunes:author>
      <itunes:subtitle>Subtitle2</itunes:subtitle>
      <itunes:summary>Summary2</itunes:summary>
      <itunes:image>image2.jpg</itunes:image>
      <enclosure url="http://example.com/podcast2.m4a" length="655555" type="audio/x-m4a"/>
      <guid>http://example.com/archive/aae2.m4a</guid>
      <pubDate>Wed, 15 Jul 2011 19:00:00 GMT</pubDate>
      <itunes:duration>11:20</itunes:duration>
      <itunes:keywords>foo, bar</itunes:keywords>
    </item>
  </channel>
</rss>
```
 
#### Example 11: Custom filter for XML entities, or whatever
 
```javascript
jstoxml.toXML({
    foo: '<a>',
    bar: '"b"',
    baz: '\'&whee\''
},
{
    filter: {
        '<': '&lt;', 
        '>': '&gt;',
        '"': '&quot;',
        '\'': '&apos;',
        '&': '&amp;'
    }
});
```
 
Output:
 
```
<foo>&lt;a&gt;</foo><bar>&quot;b&quot;</bar><baz>&apos;&amp;whee&apos;</baz>
```
 
 
#### Example 12: Empty tags
 
```javascript
jstoxml.toXML({
  foo: '',
  bar: 'whee'
});
```
 
Output:
 
```
<foo></foo><bar>whee</bar>
```