API Reference
xmlbuilder2
XML builder and converter with a chainable API for constructing documents, fragments, and parsed trees.
Official Documentation ↗CompatibilityThis documentation covers version
v4.0.3. Check the official docs for the latest API surface.Installation
npm
npm install xmlbuilder2pnpm
pnpm add xmlbuilder2Methods
No methods match this filter.
Overview
xmlbuilder2 is an XML builder and converter with a chainable API for constructing documents, fragments, and parsed trees. This reference lists the most frequently used APIs with accurate signatures and runnable snippets.
Core APIs
create— Creates a new XML document builder with optional declaration and root attributes.ele— Appends a child element, optionally with attributes, to the current node.end— Serializes the tree into an XML string with optional pretty printing.
Example
const { create } = require('xmlbuilder2');
const doc = create({ version: '1.0' }).ele('note').ele('to').txt('you').up().up();
console.log(doc.end({ prettyPrint: true }));
Notes
The builder escapes text automatically, so pass raw values to txt rather than escaping them yourself. Use up() to navigate back up the tree.
Verification
Build a document containing an ampersand in text and confirm the serialized output escapes it as &.
FAQ
- How do I convert JSON to XML?
- Call the convert function with a JSON object, or build the tree explicitly with ele, att, and txt.
- How do I move back to the parent node?
- Call up() to return to the parent element, letting you keep a single fluent chain.
- Does it escape special characters automatically?
- Yes. Text passed to txt is escaped for XML, so ampersands and angle brackets are encoded safely.
Report a problem with this API docOpens a pre-filled issue on GitHub.