Jstoxml
JavaScript object to XML converter (useful for RSS, podcasts, GPX, AMP, etc)
Install / Use
/learn @davidcalhoun/JstoxmlREADME
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 and Podcasts.
This is inspired by node-jsontoxml, which was found to be a bit too rough around the edges. jstoxml attempts to fix that by being more flexible.
Requirements
Node 16+ or a code bundler that understands ES Modules.
Installation
- npm install jstoxml
Simple example
import { toXML } from 'jstoxml';
// toXML(content, config)
const content = {
a: {
foo: 'bar'
}
};
const config = {
indent: ' '
};
toXML(content, config);
/*
Output:
`<a>
<foo>bar</foo>
</a>`
*/
Configuration object options (passed as second parameter to toXML())
| Key name | Type | Default | Description |
| --------------------- | ------------------- | ------------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| indent | string | | Indent string, repeated n times (where n=tree depth). |
| header | string, boolean | | Outputs a simple XML 1.0 UTF-8 header when true. Can also be set to a custom string. |
| attributeReplacements | object | { "<": "<", ">": ">", "&": "&", "\"": """ } | XML attribute value substrings to replace (e.g. <a attributeKey="attributeValue" />). Does not double encode HTML entities (e.g. < is preserved and NOT converted to &lt). |
| attributeFilter | function | | Filters out attributes based on user-supplied function. |
| attributeExplicitTrue | boolean | false | When true explicitly outputs true attribute value strings, e.g. <a foo='true' /> instead of <a foo />. |
| contentMap | function | | Custom map function to transform XML content. Runs after contentReplacements. |
| contentReplacements | object | { "<": "<", ">": ">", "&": "&", "\"": """ } | XML content strings to replace (e.g. <a>This & that</a> becomes <a>This & that</a>). |
| selfCloseTags | boolean | true | Whether tags should be self-closing. |
Changelog
Version 7.0.0
- BREAKING: only support ES Modules, drops support for UMD (CommonJS, window global) and drop the minified code from the package.
For more changelog history, see CHANGELOG.md.
Past changes
- See CHANGELOG.md for a full history of changes.
Other Examples
First you'll want to import jstoxml in your script, and assign the result to the namespace variable you want to use (in this case jstoxml):
import { toXML } from 'jstoxml';
Example 1: Simple object
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)
toXML([
{
foo: 'bar'
},
{
foo: 'bar2'
}
]);
Output:
<foo>bar</foo><foo>bar2</foo>
Example 3: Simple functions
toXML({ currentTime: () => new Date() });
Output:
<currentTime>Mon Oct 02 2017 09:34:54 GMT-0700 (PDT)</currentTime>
Example 4: XML tag attributes
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:
toXML({
text1: null,
foo: 'bar',
text2: null
});
Output:
text1<foo>bar</foo>text2
Example 6: Nested tags (with indenting)
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)
const xmlOptions = {
header: false,
indent: ' '
};
toXML(
{
ooo: {
_name: 'foo',
_attrs: {
a: 'b'
},
_content: {
_name: 'bar',
_attrs: {
c: 'd'
}
}
}
},
xmlOptions
);
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:
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.
toXML({
someNestedXML: () => {
return {
foo: 'bar'
};
}
});
Output:
<someNestedXML><foo>bar</foo></someNestedXML>
Example 9: RSS Feed
const xmlOptions = {
header: true,
indent: ' '
};
toXML(
{
_name: 'rss',
_attrs: {
version: '2.0'
},
_content: {
channel: [
{
title: 'RSS Example'
},
{
description: 'Description'
},
{
link: 'google.com'
},
{
lastBuildDate: () => new Date()
},
{
pubDate: () => new Date()
},
{
language: 'en'
},
{
item: {
title: 'Item title',
link: 'Item link',
description: 'Item Description',
pubDate: () => new Date()
}
},
{
item: {
title: 'Item2 title',
link: 'Item2 link',
description: 'Item2 Description',
pubDate: () => new Date()
}
}
]
}
},
xmlOptions
);
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 for more information)
const xmlOptions = {
header: true,
indent: ' '
};
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'
