Skip to content

Examples

Ozgur Ozcitak edited this page Nov 24, 2016 · 2 revisions

The Single-Entry Atom Feed Document

The single-entry Atom Feed Document as given in The Atom Syndication Format:

<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title type="text">dive into mark</title>
  <subtitle type="html">A &lt;em&gt;lot&lt;/em&gt; of effort went into making this effortless</subtitle>
  <updated>2005-07-31T12:29:29Z</updated>
  <id>tag:example.org,2003:3</id>
  <link rel="alternate" type="text/html" hreflang="en" href="http://example.org/"/>
  <link rel="self" type="application/atom+xml" href="http://example.org/feed.atom"/>
  <rights>Copyright (c) 2003, Mark Pilgrim</rights>
  <generator uri="http://www.example.com/" version="1.0">Example Toolkit</generator>
  <entry>
    <title>Atom draft-07 snapshot</title>
    <link rel="alternate" type="text/html" href="http://example.org/2005/04/02/atom"/>
    <link rel="enclosure" type="audio/mpeg" length="1337" href="http://example.org/audio/ph34r_my_podcast.mp3"/>
    <id>tag:example.org,2003:3.2397</id>
    <updated>2005-07-31T12:29:29Z</updated>
    <published>2003-12-13T08:29:29-04:00</published>
    <author>
      <name>Mark Pilgrim</name>
      <uri>http://example.org/</uri>
      <email>[email protected]</email>
    </author>
    <contributor>
      <name>Sam Ruby</name>
    </contributor>
    <contributor>
      <name>Joe Gregorio</name>
    </contributor>
    <content type="xhtml" xml:lang="en" xml:base="http://diveintomark.org/">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
          <i>[Update: The Atom draft is finished.]</i>
        </p>
      </div>
    </content>
  </entry>
</feed>

Using create and friends:

var builder = require('xmlbuilder');

var feed = builder.create('feed', { encoding: 'utf-8' })
  .att('xmlns', 'http://www.w3.org/2005/Atom')
  .ele('title', { 'type': 'text'}, 'dive into mark').up()
  .ele('subtitle', { 'type': 'html' }, 'A <em>lot</em> of effort went into making this effortless').up()
  .ele('updated', '2005-07-31T12:29:29Z').up()
  .ele('id', 'tag:example.org,2003:3').up()
  .ele('link', { 'rel': 'alternate', 'type': 'text/html', 'hreflang': 'en', 'href': 'http://example.org/' }).up()
  .ele('link', { 'rel': 'self', 'type': 'application/atom+xml', 'href': 'http://example.org/feed.atom' }).up()
  .ele('rights', 'Copyright (c) 2003, Mark Pilgrim').up()
  .ele('generator', { 'uri': 'http://www.example.com/', 'version': '1.0' }, 'Example Toolkit').up()
  .ele('entry')
    .ele('title', 'Atom draft-07 snapshot').up()
    .ele('link', { 'rel': 'alternate', 'type': 'text/html', 'href': 'http://example.org/2005/04/02/atom' }).up()
    .ele('link', { 'rel': 'enclosure', 'type': 'audio/mpeg', 'length': '1337', 'href': 'http://example.org/audio/ph34r_my_podcast.mp3' }).up()
    .ele('id', 'tag:example.org,2003:3.2397').up()
    .ele('updated', '2005-07-31T12:29:29Z').up()
    .ele('published', '2003-12-13T08:29:29-04:00').up()
    .ele('author')
      .ele('name', 'Mark Pilgrim').up()
      .ele('uri', 'http://example.org/').up()
      .ele('email', '[email protected]').up()
    .up()
    .ele('contributor')
      .ele('name', 'Sam Ruby').up()
    .up()
    .ele('contributor')
      .ele('name', 'Joe Gregorio').up()
    .up()
    .ele('content', { 'type': 'xhtml', 'xml:lang': 'en', 'xml:base': 'http://diveintomark.org/' })
      .ele('div', { 'xmlns': 'http://www.w3.org/1999/xhtml' })
        .ele('p')
          .ele('i', '[Update: The Atom draft is finished.]').up()
        .up()
      .up()
    .up()
  .up()

console.log(feed.end({ pretty: true }));

Using JS objects:

var builder = require('xmlbuilder');

var feedObj = { 
  'feed': {
    '@xmlns': 'http://www.w3.org/2005/Atom',
    'title': { '@type': 'text', '#text': 'dive into mark' },
    'subtitle': { '@type': 'html', '#text': 'A <em>lot</em> of effort went into making this effortless' },
    'updated': '2005-07-31T12:29:29Z',
    'id': 'tag:example.org,2003:3',
    'link': [
      { '@rel': 'alternate', '@type': 'text/html', '@hreflang': 'en', '@href': 'http://example.org/' },
      { '@rel': 'self', '@type': 'application/atom+xml', '@href': 'http://example.org/feed.atom' }
    ],
    'rights': 'Copyright (c) 2003, Mark Pilgrim',
    'generator': { '@uri': 'http://www.example.com/', '@version': '1.0', '#text': 'Example Toolkit' },
    'entry': {
      'title': 'Atom draft-07 snapshot',
      'link': [
        { '@rel': 'alternate', '@type': 'text/html', '@href': 'http://example.org/2005/04/02/atom' },
        { '@rel': 'enclosure', '@type': 'audio/mpeg', '@length': '1337', '@href': 'http://example.org/audio/ph34r_my_podcast.mp3' }
      ],
      'id': 'tag:example.org,2003:3.2397',
      'updated': '2005-07-31T12:29:29Z',
      'published': '2003-12-13T08:29:29-04:00',
      'author': {
        'name': 'Mark Pilgrim',
        'uri': 'http://example.org/',
        'email': '[email protected]'
      },
      'contributor' : [
        { 'name': 'Sam Ruby' },
        { 'name': 'Joe Gregorio' }
      ],
      'content': { 
        '@type': 'xhtml', 
        '@xml:lang': 'en', 
        '@xml:base': 'http://diveintomark.org/',
        'div': { 
          '@xmlns': 'http://www.w3.org/1999/xhtml',
          'p': {
            'i': '[Update: The Atom draft is finished.]'
          }
        }
      }
    }
  }
}

var feed = builder.create(feedObj, { encoding: 'utf-8' })
console.log(feed.end({ pretty: true }));
Clone this wiki locally