XML
Type Reference
Core TypeScript types for XML parsing and serialization
Element
The core type representing an XML element in the parsed tree:
interface Element {
type: "element";
name: string;
attributes?: Record<string, string>;
elements?: Element[];
}
Text nodes use a different structure:
interface TextElement {
type: "text";
text: string;
}
Creating Elements
const paragraph: Element = {
type: "element",
name: "w:p",
elements: [
{
type: "element",
name: "w:r",
elements: [{ type: "text", text: "Hello World" }],
},
],
};
Attributes
interface Attributes {
[key: string]: string;
}
A simple key-value map for XML attributes.
XmlObject
The node-xml compatible format used by the xml() function:
interface XmlObject {
[elementName: string]: (XmlObject | string)[];
}
Example:
const obj: XmlObject = {
"w:p": [{ "w:r": [{ "w:t": ["Hello"] }] }],
};
XmlAttrs
Attribute format for node-xml compatible objects:
interface XmlAttrs {
[attrName: string]: string | number | boolean;
}
Option Types
Xml2JsOptions
Options for the parse() function:
interface Xml2JsOptions {
trim?: boolean;
sanitize?: boolean;
nativeTypeAttributes?: boolean;
ignoreDeclaration?: boolean;
ignoreComment?: boolean;
ignoreCdata?: boolean;
ignoreDoctype?: boolean;
ignoreText?: boolean;
captureSpacesBetweenElements?: boolean;
compact?: boolean;
}
Js2XmlOptions
Options for the stringify() function:
interface Js2XmlOptions {
spaces?: number | string;
fullTagEmptyElement?: boolean;
ignoreDeclaration?: boolean;
ignoreComment?: boolean;
ignoreCdata?: boolean;
ignoreDoctype?: boolean;
ignoreText?: boolean;
attributeValueFn?: (val: string, name: string) => string;
}