
Security News
NVD Concedes Inability to Keep Pace with Surging CVE Disclosures in 2025
Security experts warn that recent classification changes obscure the true scope of the NVD backlog as CVE volume hits all-time highs.
dynamic-xml-builder
Advanced tools
Build XML dynamically.
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...
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.
Since this is based on ES6 proxies, then ES6 support is required:
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)
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 |
node test
MIT
FAQs
dynamic XML builder
The npm package dynamic-xml-builder receives a total of 126 weekly downloads. As such, dynamic-xml-builder popularity was classified as not popular.
We found that dynamic-xml-builder demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Security experts warn that recent classification changes obscure the true scope of the NVD backlog as CVE volume hits all-time highs.
Security Fundamentals
Attackers use obfuscation to hide malware in open source packages. Learn how to spot these techniques across npm, PyPI, Maven, and more.
Security News
Join Socket for exclusive networking events, rooftop gatherings, and one-on-one meetings during BSidesSF and RSA 2025 in San Francisco.