### 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.
### Installation
- npm install jstoxml
### Changelog
#### Version 2.2.0
* Initial support for XML comments ([#47](https://github.com/davidcalhoun/jstoxml/issues/47))
#### Version 2.1.1
* Fix for [#48](https://github.com/davidcalhoun/jstoxml/issues/48) (various 0-depth issues, bad "is output start" logic)
#### Version 2.0.0 (breaking)
- New: automatic entity escaping for `&`, `<`, and `>` characters. In addition, quotes `"` in attributes are also escaped (see [#41](/../../issues/41)). Prior to this, users [had to provide their own filter manually](https://github.com/davidcalhoun/jstoxml/issues/4#issuecomment-19165730). Note that `jstoxml` makes an effort not to escape entities that appear to have already been encoded, to prevent double-encoding issues.
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
// Node
const { toXML } = require("jstoxml");
// Browser (with the help of something like Webpack or Rollup)
import { toXML } from "jstoxml";
// Browser global fallback (requires no bundler)
var toXML = window.jstoxml.toXML;
```
#### Example 1: Simple object
```javascript
toXML({
foo: "bar",
foo2: "bar2",
});
```
Output:
```
<foo>bar</foo><foo2>bar2</foo2>
```
Note: because JavaScript doesn't allow duplicate key names, only the last defined key will be outputted. If you need duplicate keys, please use an array instead (see Example 2 below).
#### Example 2: Simple array (needed for duplicate keys)
```javascript
toXML([
{
foo: "bar",
},
{
foo: "bar2",
},
]);
```
Output:
```
<foo>bar</foo><foo>bar2</foo>
```
#### Example 3: Simple functions
```javascript
toXML({ currentTime: () => new Date() });
```
Output:
```
<currentTime>Mon Oct 02 2017 09:34:54 GMT-0700 (PDT)</currentTime>
```
#### Example 4: XML tag attributes
```javascript
toXML({
_name: "foo",
_content: "bar",
_attrs: {
a: "b",
c: "d",
},
});
```
Output:
```
<fooa="b"c="d">bar</foo>
```
#### Example 5: Tags mixed with text content
To output text content, set a key to null:
```javascript
toXML({
text1: null,
foo: "bar",
text2: null,
});
```
Output:
```
text1<foo>bar</foo>text2
```
#### Example 6: Nested tags (with indenting)
```javascript
const xmlOptions = {
header: false,
indent: " ",
};
toXML(
{
a: {
foo: "bar",
foo2: "bar2",
},
},
xmlOptions
);
```
Output:
```
<a>
<foo>bar</foo>
<foo2>bar2</foo2>
</a>
```
#### Example 7: Nested tags with attributes (with indenting)
```javascript
const xmlOptions = {
header: false,
indent: " ",
};
toXML(
{
ooo: {
_name: "foo",
_attrs: {
a: "b",
},
_content: {
_name: "bar",
_attrs: {
c: "d",
},
},
},
},
xmlOptions
);
```
Output:
```
<ooo>
<fooa="b">
<barc="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
const bar = {
_name: "bar",
_attrs: {
c: "d",
},
};
const foo = {
_name: "foo",
_attrs: {
a: "b",
},
_content: bar,
};
const xmlOptions = {
header: false,
indent: " ",
};
return toXML(
{
ooo: foo,
},
xmlOptions
);
```
#### Example 8: Complex functions
Function outputs will be processed (fed back into toXML), meaning that you can output objects that will in turn be converted to XML.