Documentation / @sw-file-lib/xml / XmlWriter
Class: XmlWriter
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
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
new XmlWriter(options?: XmlWriterOptions): XmlWriter;Defined in: packages/xml/src/writer/index.ts:65
Parameters
| Parameter | Type |
|---|---|
options? | XmlWriterOptions |
Returns
XmlWriter
Methods
begin()
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
| Parameter | Type | Description |
|---|---|---|
name | string | The name of the XML element to begin. |
attributes? | XmlAttributes | Attributes for the XML element. |
Returns
void
end()
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
| Parameter | Type | Description |
|---|---|---|
name | string | The 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()
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
| Parameter | Type | Description |
|---|---|---|
name | string | The name of the XML element to create. |
attributes? | XmlAttributes | Attributes for the XML element. |
Returns
void
element()
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
| Parameter | Type | Description |
|---|---|---|
name | string | The name of the XML element to create. |
attributes | XmlAttributes | undefined | Attributes for the XML element. |
children | (writer: XmlWriter) => void | A function that takes a XmlWriter instance and writes child elements to it. |
Returns
void
Example
writer.element("parent", { attr: "value" }, (w) => {
w.empty("child", { childAttr: "childValue" });
});comment()
comment(text: string): void;Defined in: packages/xml/src/writer/index.ts:153
Add a comment to the XML output.
Parameters
| Parameter | Type | Description |
|---|---|---|
text | string | The text of the comment to add. |
Returns
void
toString()
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.