0.1.10 • Published 4 months ago

dynamic-xml-builder v0.1.10

Weekly downloads
39
License
MIT
Repository
github
Last release
4 months ago

dynamic-xml-builder

Intro

Dynamically create XML from JS objects.

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

const data = new XMLObject('data');

data.user._type = 'person'
data.user.email = 'john@johnjohnnyandjohnson.com'
data.user.aliases.alias = ['John', 'Johnny', 'Johnson']
data.toXML()
<data>
    <user type="person">
        <email>john@johnjohnnyandjohnson.com</email>
        <aliases>
            <alias>John</alias>
            <alias>Johnny</alias>
            <alias>Johnson</alias>
        </aliases>
    </user>
</data>

Examples

PS. The examples use HTML because it's a well-known XML.

Object manipulation:

const XMLObject = require('dynamic-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 assign a property in a non-existing path
xml.body.div.div.p = 'great'
<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>

Configure output formatting:

xml.toXML({
    indent: 2, newLine: '\n'
})
<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>

Output a plain object:

xml.head.toObject()
{ head: { meta: { _charset: 'utf-8' }, title: 'example' } }

When you need to directy assign value to element you can use a value selector (_value by default):

var xml = new XMLObject('a')
xml._href = 'https://www.example.com'
xml._value = 'Click me!'
<a href="https://www.example.com">Click me!</a>

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

NameDefaultUsageDescription
attrSel"_"constructorused to identify attributes (attrSel + attributeName, i.e. "_charset")
valueSel"_value"constructorused to set the element value directly
defVal""constructordefault value to use when element value has not been provided
indent"\t"toXMLindent definition, can be any string
newLine"\r\n"toXMLnewline definition, can be any string
attrKeynulltoXMLwhen provided, will group the attributes of an element under attrKey object
declarationnulltoXMLprovide true for the default declaration, or any string to override it
selfClosetruetoXMLprovice false to disable self-closing tags

Licence

MIT

0.1.10

4 months ago

0.1.9

4 months ago

0.1.8

2 years ago

0.1.7

3 years ago

0.1.6

5 years ago

0.1.5

5 years ago

0.1.4

5 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago