XML

Type Reference

Core TypeScript types for XML parsing and serialization

Element

The core type representing any node in the parsed tree (elements, text, comments, CDATA, processing instructions). All fields are optional — an element typically uses name, attributes, and elements:

interface Element {
  declaration?: { attributes?: DeclarationAttributes };
  instruction?: string;
  attributes?: Attributes;
  cdata?: string;
  doctype?: string;
  comment?: string;
  text?: string | number | boolean;
  type?: string;
  name?: string;
  elements?: Element[];
  parent?: Element;
}

Text content

Text is not a separate node type. The text of <w:t>Hello</w:t> is stored on the element's own text field:

// parsed <w:t>Hello</w:t>
{ type: "element", name: "w:t", text: "Hello" }

Read it with textOf(element) (returns "" when absent). There is no TextElement interface.

Creating Elements

Put text directly on the element's text field — do not create { type: "text" } children:

const paragraph: Element = {
  type: "element",
  name: "w:p",
  elements: [
    {
      type: "element",
      name: "w:r",
      elements: [{ type: "element", name: "w:t", text: "Hello World" }],
    },
  ],
};

Attributes

interface Attributes {
  [key: string]: string | number | undefined;
}

A simple key-value map for XML attributes.

XmlObject

The node-xml compatible format used by the low-level xml() function. Tag names map to content descriptors — an _attr/_cdata object, an atomic value, or an array of these:

type XmlAtom = string | number | boolean | null;
type XmlObject = { [tag: string]: ElementObject | XmlDesc } | XmlDesc;

type XmlDesc =
  | { _attr: XmlAttrs }
  | { _cdata: string }
  | { _attr: XmlAttrs; _cdata: string }
  | XmlAtom
  | XmlAtom[];

Example:

const obj: XmlObject = {
  "w:p": [{ "w:r": [{ "w:t": ["Hello"] }] }],
};

XmlAttrs

Attribute map for node-xml compatible objects (values may be null):

interface XmlAttrs {
  [attrName: string]: string | number | boolean | null;
}

Option Types

Xml2JsOptions

Options for the xml2js() 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 js2xml() function:

interface Js2XmlOptions {
  spaces?: number | string;
  fullTagEmptyElement?: boolean;
  ignoreDeclaration?: boolean;
  ignoreComment?: boolean;
  ignoreCdata?: boolean;
  ignoreDoctype?: boolean;
  ignoreText?: boolean;
  attributeValueFn?: (val: string, name: string) => string;
}
Copyright © 2026