Skip to content

k.utils.xml ​

Parse and build XML documents

Execution ContextPrerequisitesTypical Use
KScript API, XML integration scriptsNoneParsing 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 / PropertyDescriptionReturns
parse(xml)Parse an XML string into a DocumentDocument
create(rootName)Create a new Document with the given root element nameDocument
doc.stringify()Serialize an XML Document into a stringstring
el.addChild(name)Append a child element and return the new ElementElement
el.addAttribute(name, value)Add an attribute and return the Attribute objectAttribute
el.addCData(content)Append a CDATA section to the elementvoid
el.removeChild(name)Remove the first matching child element by namevoid
el.removeAttribute(name)Remove an attribute by namevoid

parse() ​

Parses an XML string into a Document.

ParameterTypeRequiredDescription
xmlstringYesXML 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.

ParameterTypeRequiredDescription
rootNamestringYesRoot 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().

MemberTypeDescription
rootElementRoot Element
declarationDeclarationXML declaration (version, encoding, and standalone; accessing its properties throws if the parsed XML lacked an <?xml ...?> prolog)
stringify()() => stringSerialize 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 / MethodDescription
name / valueElement name and text (value is null when child elements exist)
children / attributesChild 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
MethodParameterReturn
addChild(name)stringElement
addAttribute(name, value)string, stringAttribute
addCData(content)stringvoid
removeChild(name)stringvoid
removeAttribute(name)stringvoid

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;
}