Skip to content

k.utils.xml

Parse and build XML documents

Overview

k.utils.xml is based on the XDocument package and provides parse, create and document/element/attribute operations.

parse()

Parses the XML string as Document.

ParameterTypeRequiredDescription
xmlstringYesXML string

Returns: Document.

ts
const doc = k.utils.xml.parse("<root><item id=\"1\">a</item></root>")
const xml = doc.stringify()

create()

Creates a new document starting with the specified root element name.

ParameterTypeRequiredDescription
rootNamestringYesRoot element name

Returns: Document.

ts
const doc = k.utils.xml.create("root")
doc.root.addChild("item")

Document

MemberDescription
rootRoot Element
declarationXML declaration
stringify()serialize to string

Element

members/methodsDescription
name / valueElement name and text
children / attributesChild elements and attribute arrays
addChild(name)Add child element
addAttribute(name, value)Add properties
addCData(content)Add CDATA
removeChild(name) / removeAttribute(name)delete
MethodParameterReturn
addChild(name)stringElement
addAttribute(name, value)string, stringAttribute
addCData(content)stringvoid
removeChild(name)stringvoid
removeAttribute(name)stringvoid
ts
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()