function
XML.stringify
Serialize one element to XML: a NodeInput tree (any object with a string name and a children or attributes property), or a compact object with exactly one key naming the root element whose value follows the Element conventions.
The result is that element's markup only — no XML declaration and no document type declaration; prepend them as text when writing a file (
'<?xml version="1.0" encoding="UTF-8"?>). Because of that, results can be concatenated inside an enclosing element.
' + XML.stringify(doc)
The output is well-formed or stringify throws. & < > are escaped everywhere; ", tabs and newlines in attribute values, and carriage returns anywhere, are written as character references so they survive being parsed again. It throws for element, attribute or processing instruction names that are not XML names; for characters XML cannot contain (U+0000, other C0 controls except tab/newline/carriage return, U+FFFE, U+FFFF, unpaired surrogates); for -- inside a comment or ?> inside processing-instruction data; for an array at the root or inside another array; and for circular structures.
Strings, numbers, booleans and bigints become text via String(), a Date its ISO string; null becomes an empty element (or leaves an attribute out); undefined, functions and symbols are skipped, as are symbol-keyed, non-enumerable and inherited properties. In the compact shape an array is one element per item and any other object is a child element.
XML.parse(XML.stringify(value)) deep-equals value for anything XML.parse returned, in either shape.
The element to serialize
Reserved; must be undefined or null
Indentation for element-only content, as in JSON.stringify: a number of spaces (at most 10) or a string (its first 10 characters). An element with any text child is written on one line so character data is unchanged.
The XML, or undefined if value is undefined, a function, or a symbol
import { XML } from "bun";
XML.stringify({ order: { "@id": "A1", item: ["Tea", "Mug"], paid: null } });
// '<order id="A1"><item>Tea</item><item>Mug</item><paid/></order>'
XML.stringify({ name: "p", attributes: { class: "x" }, children: ["Hi ", { name: "b", children: ["!"] }] }, null, 2);
// '<p class="x">Hi <b>!</b></p>'Serialize one element to XML: a NodeInput tree (any object with a string name and a children or attributes property), or a compact object with exactly one key naming the root element whose value follows the Element conventions.
The result is that element's markup only — no XML declaration and no document type declaration; prepend them as text when writing a file (
'<?xml version="1.0" encoding="UTF-8"?>). Because of that, results can be concatenated inside an enclosing element.
' + XML.stringify(doc)
The output is well-formed or stringify throws. & < > are escaped everywhere; ", tabs and newlines in attribute values, and carriage returns anywhere, are written as character references so they survive being parsed again. It throws for element, attribute or processing instruction names that are not XML names; for characters XML cannot contain (U+0000, other C0 controls except tab/newline/carriage return, U+FFFE, U+FFFF, unpaired surrogates); for -- inside a comment or ?> inside processing-instruction data; for an array at the root or inside another array; and for circular structures.
Strings, numbers, booleans and bigints become text via String(), a Date its ISO string; null becomes an empty element (or leaves an attribute out); undefined, functions and symbols are skipped, as are symbol-keyed, non-enumerable and inherited properties. In the compact shape an array is one element per item and any other object is a child element.
XML.parse(XML.stringify(value)) deep-equals value for anything XML.parse returned, in either shape.
The element to serialize
Reserved; must be undefined or null
Indentation for element-only content, as in JSON.stringify: a number of spaces (at most 10) or a string (its first 10 characters). An element with any text child is written on one line so character data is unchanged.
The XML, or undefined if value is undefined, a function, or a symbol
import { XML } from "bun";
XML.stringify({ order: { "@id": "A1", item: ["Tea", "Mug"], paid: null } });
// '<order id="A1"><item>Tea</item><item>Mug</item><paid/></order>'
XML.stringify({ name: "p", attributes: { class: "x" }, children: ["Hi ", { name: "b", children: ["!"] }] }, null, 2);
// '<p class="x">Hi <b>!</b></p>'Referenced types
interface Document
A parsed document in the compact shape: exactly one key, the root element's name. This is also what importing an .xml file evaluates to.
interface NodeInput
A Node as stringify accepts it: attributes and children may be omitted, scalars may stand where text goes, and null/undefined entries are skipped.