Skip to content

k.utils.xml ​

解析与构建 XML 文档

概述 ​

k.utils.xml 基于 XDocument 封装,提供 parse、create 及文档/元素/属性操作。

parse() ​

解析 XML 字符串为 Document。

参数类型必填说明
xmlstring是XML 字符串

返回: Document。

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

create() ​

创建以指定根元素名开头的新文档。

参数类型必填说明
rootNamestring是根元素名称

返回: Document。

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

Document ​

成员说明
root根 Element
declarationXML 声明
stringify()序列化为字符串

Element ​

成员 / 方法说明
name / value元素名与文本
children / attributes子元素与属性数组
addChild(name)添加子元素
addAttribute(name, value)添加属性
addCData(content)添加 CDATA
removeChild(name) / removeAttribute(name)删除
方法参数返回
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()

相关文档 ​