Giter Club home page Giter Club logo

o2xml's Introduction

o2xml

Simple object to XML converter.

Install

npm install o2xml

Simple example

var o2xml = require('o2xml');
var object = {
    thing: {
        '@flag': true,
        '@number': 123,
        '@date': new Date(),
        items: {
            '@length': 2,
            item: [
                {name: 'Alexander', birth: new Date('2002-12-09')},
                {name: 'Daniel', birth: new Date('1991-03-22')}
            ]
        }
    }
}

console.log(o2xml.transform(object, {pretty: true}));

converts to:

<thing flag="1" number="123" date="2016-03-18T14:18:00.563Z">
  <items length="2">
    <item>
      <name>Alexander</name>
      <birth>2002-12-09T00:00:00.000Z</birth>
    </item>
    <item>
      <name>Daniel</name>
      <birth>1991-03-22T00:00:00.000Z</birth>
    </item>
  </items>
</thing>

Options

  • pretty: true|false make pretty XML (default: false).
  • indent: \t spacing (default: two spaces).
  • declaration: true write the XML declaration (default: false).
  • formatters: {string:fn, boolean:fn, number:fn, date:fn} format and escape functions.

Mix strings, nodes and attributes

var o2xml = require('o2xml');
var object = {
    div: {
        '@id': 'id',
        '#text': 'Hello, ',
        span: {
            '@style': 'font-weight: bold',
            '#text': 'World'
        },
        '#text2': '!'
    }
}

console.log(o2xml.transform(object));

converts to:

<div id="id">Hello, <span style="font-weight: bold">World</span>!</div>

Note

  • Text escape by default (amp, quote, single quote, left/right angle brackets).
  • Node.js Buffer will be converted to base64 encoded string.
  • String nodes must start with # and key must be unique in space.

o2xml's People

Contributors

dependabot[bot] avatar izatop avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.