Skip to content

Documentation / @sw-file-lib/xml / XmlWriter

Class: XmlWriter ​

ts
class XmlWriter {
  constructor(options?: XmlWriterOptions);
  begin(name: string, attributes?: XmlAttributes): void;
  end(name: string): void;
  empty(name: string, attributes?: XmlAttributes): void;
  element(name: string, attributes: XmlAttributes | undefined, children: (writer: XmlWriter) => void): void;
  comment(text: string): void;
  toString(): string;
}

Defined in: packages/xml/src/writer/index.ts:60

A utility class for generating XML content programmatically.

Example ​

ts
const writer = new XmlWriter({ indent: 2 });
writer.begin("root");
writer.element("child", { attr: "value" }, (w) => {
  w.comment("This is a comment");
});
writer.end("root");
console.log(writer.toString());

See ​

XmlWriterOptions for configuration options.

Constructors ​

Constructor ​

ts
new XmlWriter(options?: XmlWriterOptions): XmlWriter;

Defined in: packages/xml/src/writer/index.ts:65

Parameters ​

ParameterType
options?XmlWriterOptions

Returns ​

XmlWriter

Methods ​

begin() ​

ts
begin(name: string, attributes?: XmlAttributes): void;

Defined in: packages/xml/src/writer/index.ts:79

Begin an XML element with the specified name and optional attributes.

Parameters ​

ParameterTypeDescription
namestringThe name of the XML element to begin.
attributes?XmlAttributesAttributes for the XML element.

Returns ​

void


end() ​

ts
end(name: string): void;

Defined in: packages/xml/src/writer/index.ts:90

Close the most recently opened XML element with the specified name.

Parameters ​

ParameterTypeDescription
namestringThe name of the XML element to close.

Returns ​

void

Throws ​

An error if the closing tag does not match the most recently opened element.


empty() ​

ts
empty(name: string, attributes?: XmlAttributes): void;

Defined in: packages/xml/src/writer/index.ts:110

Create an empty XML element with the specified name and optional attributes.

Parameters ​

ParameterTypeDescription
namestringThe name of the XML element to create.
attributes?XmlAttributesAttributes for the XML element.

Returns ​

void


element() ​

ts
element(
   name: string, 
   attributes: XmlAttributes | undefined, 
   children: (writer: XmlWriter) => void): void;

Defined in: packages/xml/src/writer/index.ts:128

Create an XML element with the specified name, attributes, and child elements.

Parameters ​

ParameterTypeDescription
namestringThe name of the XML element to create.
attributesXmlAttributes | undefinedAttributes for the XML element.
children(writer: XmlWriter) => voidA function that takes a XmlWriter instance and writes child elements to it.

Returns ​

void

Example ​

ts
writer.element("parent", { attr: "value" }, (w) => {
  w.empty("child", { childAttr: "childValue" });
});

comment() ​

ts
comment(text: string): void;

Defined in: packages/xml/src/writer/index.ts:153

Add a comment to the XML output.

Parameters ​

ParameterTypeDescription
textstringThe text of the comment to add.

Returns ​

void


toString() ​

ts
toString(): string;

Defined in: packages/xml/src/writer/index.ts:162

Generate the final XML string from the written elements.

Returns ​

string

Throws ​

An error if there are unclosed elements when this method is called.