k.utils.xml
Parse and build XML documents
| Execution Context | Prerequisites | Typical Use |
|---|---|---|
| KScript API, XML integration scripts | None | Parsing external XML payloads and constructing XML feeds or API bodies |
k.utils.xml is built on XDocument, providing parse, create, and Document, Element, Attribute, and CDATA helpers.
Quick Start
js
k.api.get("buildXml", () => {
const doc = k.utils.xml.create("items")
const item = doc.root.addChild("item")
item.addAttribute("id", "1001")
item.value = "Kooboo"
return { xml: doc.stringify() }
})Quick Reference
| Method / Property | Description | Returns |
|---|---|---|
parse(xml) | Parse an XML string into a Document | Document |
create(rootName) | Create a new Document with the given root element name | Document |
doc.stringify() | Serialize an XML Document into a string | string |
el.addChild(name) | Append a child element and return the new Element | Element |
el.addAttribute(name, value) | Add an attribute and return the Attribute object | Attribute |
el.addCData(content) | Append a CDATA section to the element | void |
el.removeChild(name) | Remove the first matching child element by name | void |
el.removeAttribute(name) | Remove an attribute by name | void |
parse()
Parses an XML string into a Document.
| Parameter | Type | Required | Description |
|---|---|---|---|
xml | string | Yes | XML string |
Returns: Document.
js
const doc = k.utils.xml.parse("<root><item id=\"1\">a</item></root>")
const xml = doc.stringify()create()
Creates a new XML document starting with the specified root element name.
| Parameter | Type | Required | Description |
|---|---|---|---|
rootName | string | Yes | Root element name |
Returns: Document.
js
const doc = k.utils.xml.create("root")
doc.root.addChild("item")Document
XML document object returned by parse() or create().
| Member | Type | Description |
|---|---|---|
root | Element | Root Element |
declaration | Declaration | XML declaration (version, encoding, and standalone; accessing its properties throws if the parsed XML lacked an <?xml ...?> prolog) |
stringify() | () => string | Serialize to string |
Attribute members: name (string), value (string); element.value returns null when the element has child elements.
Element
XML element node supporting child elements, attributes, text values, and CDATA sections.
| Member / Method | Description |
|---|---|
name / value | Element name and text (value is null when child elements exist) |
children / attributes | Child elements and attribute arrays |
addChild(name) | Add child element |
addAttribute(name, value) | Add attribute |
addCData(content) | Add CDATA section |
removeChild(name) / removeAttribute(name) | Delete the first matching child element or attribute by name |
| Method | Parameter | Return |
|---|---|---|
addChild(name) | string | Element |
addAttribute(name, value) | string, string | Attribute |
addCData(content) | string | void |
removeChild(name) | string | void |
removeAttribute(name) | string | void |
Returns: addChild() returns Element; addAttribute() returns Attribute; other mutating methods return void.
js
const doc = k.utils.xml.create("items")
const item = doc.root.addChild("item")
item.addAttribute("id", "1001")
item.value = "Kooboo"
doc.root.addCData("raw <xml> content")
doc.root.removeAttribute("draft")
const xml = doc.stringify()TypeScript Definitions
ts
interface Xml {
parse(xml: string): Document;
create(rootName: string): Document;
}
interface Document {
declaration: Declaration;
root: Element;
stringify(): string;
}
interface Declaration {
version: string;
encoding: string;
standalone: string;
}
interface Element {
children: Element[];
attributes: Attribute[];
name: string;
value: string;
addAttribute(name: string, value: string): Attribute;
addChild(name: string): Element;
addCData(content: string): void;
removeAttribute(name: string): void;
removeChild(name: string): void;
}
interface Attribute {
name: string;
value: string;
}