All Projects → davidcalhoun → Jstoxml

davidcalhoun / Jstoxml

Licence: mit
JavaScript object to XML converter (useful for RSS, podcasts, GPX, AMP, etc)

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Jstoxml

Rss Parser
A lightweight RSS parser, for Node and the browser
Stars: ✭ 793 (+524.41%)
Mutual labels:  rss, xml
Winds
A Beautiful Open Source RSS & Podcast App Powered by Getstream.io
Stars: ✭ 8,530 (+6616.54%)
Mutual labels:  rss, podcast
Pygooglenews
If Google News had a Python library
Stars: ✭ 900 (+608.66%)
Mutual labels:  google, rss
django-radio
RadioCo is a radio management application that makes easy scheduling, live recording, publishing...
Stars: ✭ 26 (-79.53%)
Mutual labels:  rss, podcast
Ampify
Convert your HTML to Google AMP (Accelerated Mobile Pages)
Stars: ✭ 104 (-18.11%)
Mutual labels:  google, amp
Podcastgenerator
Open Source Podcast Publishing Solution since 2006
Stars: ✭ 344 (+170.87%)
Mutual labels:  rss, podcast
Russia It Podcast
Список русскоязычных подкастов на тему информационных технологий
Stars: ✭ 1,095 (+762.2%)
Mutual labels:  rss, podcast
podcast-feed-parser
A highly customizable package for fetching and parsing podcast feeds into simple and manageable JavaScript objects. For use with node or in the browser.
Stars: ✭ 39 (-69.29%)
Mutual labels:  rss, podcast
Laravel Sitemap
Create and generate sitemaps with ease
Stars: ✭ 1,325 (+943.31%)
Mutual labels:  google, xml
Sax Wasm
The first streamable, fixed memory XML, HTML, and JSX parser for WebAssembly.
Stars: ✭ 89 (-29.92%)
Mutual labels:  xml, jsx
podpodge
Convert YouTube playlists to audio-only RSS feeds for podcast apps to consume.
Stars: ✭ 32 (-74.8%)
Mutual labels:  rss, podcast
Laravel Amp
Package that helps you set up AMP (Accelerated Mobile Pages) using Laravel
Stars: ✭ 106 (-16.54%)
Mutual labels:  google, amp
fiet
Fiết is a RSS feed parser in Elixir, which focuses on extensibility, speed, and standard compliance
Stars: ✭ 23 (-81.89%)
Mutual labels:  rss, xml
Android Dev Sources
All those Android development sources that you need to be and stay awesome!
Stars: ✭ 434 (+241.73%)
Mutual labels:  rss, podcast
Spotify-Podcast-Feed
A service which provides Spotify podcast as RSS feed, which can be subscribed in any podcast app.
Stars: ✭ 16 (-87.4%)
Mutual labels:  rss, podcast
Snowflake
❄️ SVG in Swift
Stars: ✭ 924 (+627.56%)
Mutual labels:  rss, xml
reader
A Python feed reader library.
Stars: ✭ 290 (+128.35%)
Mutual labels:  rss, podcast
this-american-life-archive
Unofficial RSS feed for the podcast "This American Life" with episodes 1 to current
Stars: ✭ 19 (-85.04%)
Mutual labels:  rss, podcast
Ydls
youtube-dl HTTP download and transcode service
Stars: ✭ 68 (-46.46%)
Mutual labels:  rss, podcast
Plot
A DSL for writing type-safe HTML, XML and RSS in Swift.
Stars: ✭ 1,722 (+1255.91%)
Mutual labels:  rss, xml

jstoxml

Build Status npm downloads

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.

Installation

  • npm install jstoxml

Changelog

Version 2.0.0 (breaking)

  • New: automatic entity escaping for &, <, and > characters. In addition, quotes " in attributes are also escaped (see #41). Prior to this, users had to provide their own filter manually. Note that jstoxml makes an effort not to escape entities that appear to have already been encoded, to prevent double-encoding issues.
    • E.g. toXML({ foo: '1 < 2 & 2 > 1' }); // -> "<foo>1 &lt; 2 &amp; 2 &gt; 1</foo>"
    • To restore the default behavior from v1.x.x, simply pass in false to filter and attributesFilter options: toXML({ foo: '1 < 2 & 2 > 1' }, { filter: false, attributesFilter: false }); // -> "<foo>1 < 2 & 2 > 1</foo>"

Version 1.6.9

  • fix for #40. Previously top-level objects and arrays were concatenated without proper line breaks.

Version 1.4.2

  • support for handling arrays of primitives, instead of simply concatenating #33

Version 1.3.0

  • restored default module export #31

Version 1.2.0

  • refactoring and cleanup

Version 1.1.0

  • Added support for attribute filtering (see Example 11b below).

Version 1.0.0

  • Complete rewrite! The code should now be easier to understand and maintain.
  • now supports emoji/UTF8 tag attributes (needed for AMP pages - e.g. <html ⚡ lang="en">) (see example 14)
  • now supports duplicate attribute key names (see example 15)
  • Fixed: functions returning objects now have now that output passed through toXML for XML conversion
  • Fixed: empty text strings now properly output self-closing tags
  • Migrated tests to mocha

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):

// 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

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'
      },
      {
        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'
          }
        ]
      }
    ]
  }
}, xmlOptions);

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

const xmlOptions = {
  filter: {
    '<': '&lt;', 
    '>': '&gt;',
    '"': '&quot;',
    '\'': '&apos;',
    '&': '&amp;'
  }
};

toXML({
	foo: '<a>',
	bar: '"b"',
	baz: '\'&whee\''
}, xmlOptions);

Output:

<foo>&lt;a&gt;</foo><bar>&quot;b&quot;</bar><baz>&apos;&amp;whee&apos;</baz>

Example 11b: Custom filter for XML attributes

const xmlOptions = {
  attributesFilter: {
    '<': '&lt;', 
    '>': '&gt;',
    '"': '&quot;',
    '\'': '&apos;',
    '&': '&amp;'
  }
};

toXML({
  _name: 'foo',
  _attrs: { a: '<"\'&"foo>' }
}, xmlOptions);

Output:

<foo a="&lt;&quot;&apos;&amp;&quot;foo&gt;"/>

Example 12: Avoiding self-closing tags

If for some reason you want to avoid self-closing tags, you can pass in a special config option _selfCloseTag:

const xmlOptions = {
  _selfCloseTag: false
};

toXML({
  foo: '',
  bar: undefined
}, xmlOptions);

Output:

<foo></foo><bar>whee</bar>

Example 13: Custom XML header

const xmlOptions = {
  header: '<?xml version="1.0" encoding="UTF-16" standalone="yes"?>'
};

toXML({
  foo: 'bar'
}, xmlOptions);

Output:

<?xml version="1.0" encoding="UTF-16" standalone="yes"?><foo>bar</foo><foo2>bar2</foo2>

Example 14: Emoji attribute support (needed for AMP)

toXML({
  html: {
    _attrs: {
      '⚡': true
    }
  }
});

Output:

<html ⚡/>

Example 15: Duplicate attribute key support

toXML({
  html: {
    _attrs: [
      {
        lang: 'en'
      },
      {
        lang: 'klingon'
      }
    ]
  }
});

Output:

<html lang="en" lang="klingon"/>

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].