Giter VIP home page Giter VIP logo

js-xml-builder's Introduction

xml-builder

Build XML dynamically.

Some rant

Looking through the NPM trying to find an intuitive way to declare XML using JSON objects and all I see is a $#1tload of bad syntax design. Why is it so hard to first design the syntax and then the desired functionality around it? The developers are the users, one should know to program for their convenience.

So here's another attempt at an XML builder...

How easy this is

Write this

const XMLObject = require('xml-builder');

var xml = new XMLObject('html');

// assign an attribute
xml._lang = 'en';
// assign a whole object
xml.head = {
    meta: {
        _charset: 'utf-8'
    }
}
// go deeper
xml.head.title = 'example'
// or more complex
xml.body = {
    div: {
        _class: 'my-design',
        p: [
            'hello', 'how', 'are', 'you'
        ],
        br: null
    }
}
// or even do this
xml.body.div.div.p = 'great'

and with

xml.toXML();

get that:

<html lang="en">
	<head>
		<meta charset="utf-8"/>
		<title>example</title>
	</head>
	<body>
		<div class="my-design">
			<p>hello</p>
			<p>how</p>
			<p>are</p>
			<p>you</p>
			<br/>
			<div>
				<p>great</p>
			</div>
		</div>
	</body>
</html>

or with

xml.toXML({
    indent: 2, newLine: '\n'
})

this:

<html lang="en">
  <head>
    <meta charset="utf-8"/>
    <title>example</title>
  </head>
  <body>
    <div class="my-design">
      <p>hello</p>
      <p>how</p>
      <p>are</p>
      <p>you</p>
      <br/>
      <div>
        <p>great</p>
      </div>
    </div>
  </body>
</html>

Every element in the object tree (except for assigned primitive values) is an XMLObject. Therefore the same functionality applies to those objects:

xml.head.toXML()
<head>
    <meta charset="utf-8"/>
    <title>example</title>
</head>
xml.head.toObject()
{ head: { meta: { _charset: 'utf-8' }, title: 'example' } }

PS. The examples include a lot of HTML, this library is not intended for composing HTML.

Requirements

Since this is based on ES6 proxies, then ES6 support is required:

Constructor overloads

new XMLObject('html')
new XMLObject('html', {head: {}, body: {}})
new XMLObject('html', {head: {}, body: {}}, ...options)
new XMLObject({html: {head: {}, body: {}}})
new XMLObject({html: {head: {}, body: {}}}, ...options)

Options

Different options can be passed to the constructor or toXML(options) method

Name Default Usage Description
attrSel "_" constructor used to identify attributes (attrSel + attributeName, i.e. "_charset")
defVal "" constructor default value to use when element value has not been provided
indent "\t" toXML indent definition, can be any string
newLine "\r\n" toXML newline definition, can be any string
attrKey null toXML when provided, will group the attributes of an element under attrKey object
declaration null toXML provide true for the default declaration, or any string to override it

Testing

node test

Licence

MIT

js-xml-builder's People

Contributors

prettydiff avatar sysaxis avatar

Stargazers

 avatar

Watchers

 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.