• Stars
    star
    274
  • Rank 150,274 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 13 years ago
  • Updated almost 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Fast and simple Javascript-based XML generator/builder for Node projects.

xml Build Status

NPM

Fast and simple Javascript-based XML generator/builder for Node projects.

Install

$ npm install xml

API

xml(xmlObject, options)

Returns a XML string.

var xml = require('xml');
var xmlString = xml(xmlObject, options);

xmlObject

xmlObject is a normal JavaScript Object/JSON object that defines the data for the XML string.

Keys will become tag names. Values can be an array of xmlObjects or a value such as a string or number.

xml({a: 1}) === '<a>1</a>'
xml({nested: [{ keys: [{ fun: 'hi' }]}]}) === '<nested><keys><fun>hi</fun></keys></nested>'

There are two special keys:

_attr

Set attributes using a hash of key/value pairs.

xml({a: [{ _attr: { attributes: 'are fun', too: '!' }}, 1]}) === '<a attributes="are fun" too="!">1</a>'

_cdata

Value of _cdata is wrapped in xml ![CDATA[]] so the data does not need to be escaped.

xml({a: { _cdata: "i'm not escaped: <xml>!"}}) === '<a><![CDATA[i\'m not escaped: <xml>!]]></a>'

Mixed together:

xml({a: { _attr: { attr:'hi'}, _cdata: "I'm not escaped" }}) === '<a attr="hi"><![CDATA[I\'m not escaped]]></a>'

options

indent optional string What to use as a tab. Defaults to no tabs (compressed). For example you can use '\t' for tab character, or ' ' for two-space tabs.

stream Return the result as a stream.

Stream Example

var elem = xml.element({ _attr: { decade: '80s', locale: 'US'} });
var stream = xml({ toys: elem }, { stream: true });
stream.on('data', function (chunk) {console.log("data:", chunk)});
elem.push({ toy: 'Transformers' });
elem.push({ toy: 'GI Joe' });
elem.push({ toy: [{name:'He-man'}] });
elem.close();

/*
result:
data: <toys decade="80s" locale="US">
data:     <toy>Transformers</toy>
data:     <toy>GI Joe</toy>
data:     <toy>
            <name>He-man</name>
          </toy>
data: </toys>
*/

Declaration optional Add default xml declaration as first node.

options are:

  • encoding: 'value'
  • standalone: 'value'

Declaration Example

xml([ { a: 'test' }], { declaration: true })
//result: '<?xml version="1.0" encoding="UTF-8"?><a>test</a>'

xml([ { a: 'test' }], { declaration: { standalone: 'yes', encoding: 'UTF-16' }})
//result: '<?xml version="1.0" encoding="UTF-16" standalone="yes"?><a>test</a>'

Examples

Simple Example

var example1 = [ { url: 'http://www.google.com/search?aq=f&sourceid=chrome&ie=UTF-8&q=opower' } ];
console.log(XML(example1));
//<url>http://www.google.com/search?aq=f&amp;sourceid=chrome&amp;ie=UTF-8&amp;q=opower</url>

Example with attributes

var example2 = [ { url: { _attr: { hostname: 'www.google.com', path: '/search?aq=f&sourceid=chrome&ie=UTF-8&q=opower' }  } } ];
console.log(XML(example2));
//result: <url hostname="www.google.com" path="/search?aq=f&amp;sourceid=chrome&amp;ie=UTF-8&amp;q=opower"/>

Example with array of same-named elements and nice formatting

var example3 = [ { toys: [ { toy: 'Transformers' } , { toy: 'GI Joe' }, { toy: 'He-man' } ] } ];
console.log(XML(example3));
//result: <toys><toy>Transformers</toy><toy>GI Joe</toy><toy>He-man</toy></toys>
console.log(XML(example3, true));
/*
result:
<toys>
    <toy>Transformers</toy>
    <toy>GI Joe</toy>
    <toy>He-man</toy>
</toys>
*/

More complex example

var example4 = [ { toys: [ { _attr: { decade: '80s', locale: 'US'} }, { toy: 'Transformers' } , { toy: 'GI Joe' }, { toy: 'He-man' } ] } ];
console.log(XML(example4, true));
/*
result:
<toys decade="80s" locale="US">
    <toy>Transformers</toy>
    <toy>GI Joe</toy>
    <toy>He-man</toy>
</toys>
*/

Even more complex example

var example5 = [ { toys: [ { _attr: { decade: '80s', locale: 'US'} }, { toy: 'Transformers' } , { toy: [ { _attr: { knowing: 'half the battle' } }, 'GI Joe'] }, { toy: [ { name: 'He-man' }, { description: { _cdata: '<strong>Master of the Universe!</strong>'} } ] } ] } ];
console.log(XML(example5, true));
/*
result:
<toys decade="80s" locale="US">
    <toy>Transformers</toy>
    <toy knowing="half the battle">
        GI Joe
    </toy>
    <toy>
        <name>He-man</name>
        <description><![CDATA[<strong>Master of the Universe!</strong>]]></description>
    </toy>
</toys>
*/

Tests

Tests included use AVA. Use npm test to run the tests.

$ npm test

Examples

There are examples in the examples directory.

Contributing

Contributions to the project are welcome. Feel free to fork and improve. I accept pull requests when tests are included.

More Repositories

1

npm-check

Check for outdated, incorrect, and unused dependencies.
JavaScript
6,553
star
2

shortid

Short id generator. Url-friendly. Non-predictable. Cluster-compatible.
JavaScript
5,736
star
3

node-rss

RSS feed generator for Node.
JavaScript
995
star
4

grunt-notify

Automatic Notifications when Grunt tasks fail.
JavaScript
921
star
5

grunt-prompt

Add interactive UI to your Gruntfile such as lists, checkboxes, text input with filtering, and password fields, all on the command line.
JavaScript
368
star
6

observatory

Beautiful UI for showing tasks running on the command line.
JavaScript
276
star
7

changelog

Finally see what's changed when you do npm update. Changelog generates a changelog for npm modules and github repos.
JavaScript
239
star
8

space-hogs

Discover surprisingly large directories from the command line.
JavaScript
161
star
9

opowerjobs

Node.js site using Express and Connect.
JavaScript
61
star
10

logging

Lightweight informative modern console logging.
JavaScript
54
star
11

anthology

List of modules for any npm user, plus number of downloads and github stars.
JavaScript
46
star
12

captionbot

Get captions for image using Microsoft's CaptionBot
JavaScript
41
star
13

grunt-attention

Display attention-grabbing messages in the terminal.
JavaScript
26
star
14

jira-improved

Improves the Jira Agile board by showing the Epic name in the cards and hiding the Done column if you use a quick filter to hide tickets in that column.
JavaScript
24
star
15

node-atom

DEPRECATED - NOT MAINTAINED
JavaScript
20
star
16

what-dog

Get the breed of a dog from an image using Microsoft's what-dog
JavaScript
15
star
17

grunt-cat

Echo a file to the terminal. Works with text, figlets, ascii art, and full-color ansi.
JavaScript
14
star
18

random-puppy

Get a random puppy image url.
JavaScript
13
star
19

jobvite

DEPRECATED ----- Jobvite API for Node
JavaScript
12
star
20

source-map-diff

Compare source maps with cli and web output
TypeScript
7
star
21

flowdock-refined

DEPRECATED - Flowdock desktop app custom UI
CSS
5
star
22

...

really, this is a valid project name?
2
star
23

dotfiles

Vim Script
2
star
24

lean

Simple in-memory data store to lean on. Helpful when you don't need a couch.
JavaScript
2
star
25

jquery-tooltips

Easy to use image-free HTML5 tooltips for jQuery 1.5.2+.
JavaScript
2
star
26

grunt-templates-dylang

Templates for grunt-readme that dylang will be using across multiple projects.
JavaScript
1
star
27

dotconfig

My custom terminal colors, bash-it prompt, intellij and webstorm settings, etc
Shell
1
star
28

dylang.github.com

Everything you need to know about me.
1
star