A simple XML tree generator.
Example:
import std/xmltree
var g = newElement("myTag")
g.add newText("some text")
g.add newComment("this is comment")
var h = newElement("secondTag")
h.add newEntity("some entity")
let att = {"key1": "first value", "key2": "second value"}.toXmlAttributes
let k = newXmlTree("treeTag", [g, h], att)
doAssert $k == """<treeTag key1="first value" key2="second value">
<myTag>some text<!-- this is comment --></myTag>
<secondTag>&some entity;</secondTag>
</treeTag>"""
See also:XmlAttributes = StringTableRef
An alias for a string to string mapping.
Use toXmlAttributes proc to create XmlAttributes.
XmlNode = ref XmlNodeObj
An XML tree consisting of XML nodes.
Use newXmlTree proc for creating a new tree.
Source Editproc add(father, son: XmlNode) {.inline, ...raises: [], tags: [], forbids: [].}Adds the child son to father. father must be of xnElement type
See also:
Example:
var f = newElement("myTag")
f.add newText("my text")
f.add newElement("sonTag")
f.add newEntity("my entity")
assert $f == "<myTag>my text<sonTag />&my entity;</myTag>" Source Edit proc add(father: XmlNode; sons: openArray[XmlNode]) {.inline, ...raises: [],
tags: [], forbids: [].}Adds the children sons to father. father must be of xnElement type
See also:
Example:
var f = newElement("myTag")
f.add(@[newText("my text"), newElement("sonTag"), newEntity("my entity")])
assert $f == "<myTag>my text<sonTag />&my entity;</myTag>" Source Edit proc add(result: var string; n: XmlNode; indent = 0; indWidth = 2;
addNewLines = true) {.inline, ...raises: [], tags: [], forbids: [].}n to string result. Example:
var
a = newElement("firstTag")
b = newText("my text")
c = newComment("my comment")
s = ""
s.add(c)
s.add(a)
s.add(b)
assert s == "<!-- my comment --><firstTag />my text" Source Edit proc addEscaped(result: var string; s: string) {....raises: [], tags: [],
forbids: [].}proc attr(n: XmlNode; name: string): string {....raises: [], tags: [], forbids: [].}Finds the first attribute of n with a name of name. Returns "" on failure.
See also:
Example:
var j = newElement("myTag")
let att = {"key1": "first value", "key2": "second value"}.toXmlAttributes
j.attrs = att
assert j.attr("key1") == "first value"
assert j.attr("key2") == "second value" Source Edit proc attrs(n: XmlNode): XmlAttributes {.inline, ...raises: [], tags: [],
forbids: [].}Gets the attributes belonging to n.
Returns nil if attributes have not been initialised for this node.
See also:
Example:
var j = newElement("myTag")
assert j.attrs == nil
let att = {"key1": "first value", "key2": "second value"}.toXmlAttributes
j.attrs = att
assert j.attrs == att Source Edit proc attrs=(n: XmlNode; attr: XmlAttributes) {.inline, ...raises: [], tags: [],
forbids: [].}Sets the attributes belonging to n.
See also:
Example:
var j = newElement("myTag")
assert j.attrs == nil
let att = {"key1": "first value", "key2": "second value"}.toXmlAttributes
j.attrs = att
assert j.attrs == att Source Edit proc attrsLen(n: XmlNode): int {.inline, ...raises: [], tags: [], forbids: [].}Returns the number of n's attributes.
See also:
Example:
var j = newElement("myTag")
assert j.attrsLen == 0
let att = {"key1": "first value", "key2": "second value"}.toXmlAttributes
j.attrs = att
assert j.attrsLen == 2 Source Edit proc child(n: XmlNode; name: string): XmlNode {....raises: [], tags: [],
forbids: [].}n with a name of name. Returns nil on failure. Example:
var f = newElement("myTag")
f.add newElement("firstSon")
f.add newElement("secondSon")
f.add newElement("thirdSon")
assert $(f.child("secondSon")) == "<secondSon />" Source Edit proc clear(n: var XmlNode) {....raises: [], tags: [], forbids: [].}Example:
var g = newElement("myTag")
g.add newText("some text")
g.add newComment("this is comment")
var h = newElement("secondTag")
h.add newEntity("some entity")
let att = {"key1": "first value", "key2": "second value"}.toXmlAttributes
var k = newXmlTree("treeTag", [g, h], att)
doAssert $k == """<treeTag key1="first value" key2="second value">
<myTag>some text<!-- this is comment --></myTag>
<secondTag>&some entity;</secondTag>
</treeTag>"""
clear(k)
doAssert $k == """<treeTag key1="first value" key2="second value" />""" Source Edit proc delete(n: XmlNode; i: Natural) {....raises: [], tags: [], forbids: [].}Deletes the i'th child of n.
See also:
Example:
var f = newElement("myTag")
f.add newElement("first")
f.insert(newElement("second"), 0)
f.delete(0)
assert $f == """<myTag>
<first />
</myTag>""" Source Edit proc delete(n: XmlNode; slice: Slice[int]) {....raises: [], tags: [], forbids: [].}Deletes the items n[slice] of n.
See also:
Example:
var f = newElement("myTag")
f.add newElement("first")
f.insert([newElement("second"), newElement("third")], 0)
f.delete(0..1)
assert $f == """<myTag>
<first />
</myTag>""" Source Edit proc escape(s: string): string {....raises: [], tags: [], forbids: [].}Escapes s for inclusion into an XML document.
Escapes these characters:
| char | is converted to |
|---|---|
< |
< |
> |
> |
& |
& |
" |
" |
' |
' |
You can also use addEscaped proc.
Source Editproc findAll(n: XmlNode; tag: string; caseInsensitive = false): seq[XmlNode] {.
...raises: [], tags: [], forbids: [].}Example:
var
b = newElement("good")
c = newElement("bad")
d = newElement("BAD")
e = newElement("GOOD")
b.add newText("b text")
c.add newText("c text")
d.add newText("d text")
e.add newText("e text")
let a = newXmlTree("father", [b, c, d, e])
assert $(a.findAll("good")) == "@[<good>b text</good>]"
assert $(a.findAll("BAD")) == "@[<BAD>d text</BAD>]"
assert $(a.findAll("good", caseInsensitive = true)) == "@[<good>b text</good>, <GOOD>e text</GOOD>]"
assert $(a.findAll("BAD", caseInsensitive = true)) == "@[<bad>c text</bad>, <BAD>d text</BAD>]" Source Edit proc findAll(n: XmlNode; tag: string; result: var seq[XmlNode];
caseInsensitive = false) {....raises: [], tags: [], forbids: [].}Iterates over all the children of n returning those matching tag.
Found nodes satisfying the condition will be appended to the result sequence.
Example:
var
b = newElement("good")
c = newElement("bad")
d = newElement("BAD")
e = newElement("GOOD")
b.add newText("b text")
c.add newText("c text")
d.add newText("d text")
e.add newText("e text")
let a = newXmlTree("father", [b, c, d, e])
var s = newSeq[XmlNode]()
a.findAll("good", s)
assert $s == "@[<good>b text</good>]"
s.setLen(0)
a.findAll("good", s, caseInsensitive = true)
assert $s == "@[<good>b text</good>, <GOOD>e text</GOOD>]"
s.setLen(0)
a.findAll("BAD", s)
assert $s == "@[<BAD>d text</BAD>]"
s.setLen(0)
a.findAll("BAD", s, caseInsensitive = true)
assert $s == "@[<bad>c text</bad>, <BAD>d text</BAD>]" Source Edit proc innerText(n: XmlNode): string {....raises: [], tags: [], forbids: [].}n:n is xnText or xnEntity, returns its content.n is xnElement, runs recursively on each child node and concatenates the results.See also:
Example:
var f = newElement("myTag")
f.add newText("my text")
f.add newComment("my comment")
f.add newEntity("my entity")
assert $f == "<myTag>my text<!-- my comment -->&my entity;</myTag>"
assert innerText(f) == "my textmy entity" Source Edit proc insert(father, son: XmlNode; index: int) {.inline, ...raises: [], tags: [],
forbids: [].}Inserts the child son to a given position in father.
father must be of xnElement kind.
See also:
Example:
var f = newElement("myTag")
f.add newElement("first")
f.insert(newElement("second"), 0)
assert $f == """<myTag>
<second />
<first />
</myTag>""" Source Edit proc insert(father: XmlNode; sons: openArray[XmlNode]; index: int) {.inline,
...raises: [], tags: [], forbids: [].}Inserts the children openArray`sons` to a given position in father.
father must be of xnElement kind.
See also:
Example:
var f = newElement("myTag")
f.add newElement("first")
f.insert([newElement("second"), newElement("third")], 0)
assert $f == """<myTag>
<second />
<third />
<first />
</myTag>""" Source Edit proc newElement(tag: sink string): XmlNode {....raises: [], tags: [], forbids: [].}Creates a new XmlNode of kind xnElement with the given tag.
See also:
Example:
var a = newElement("firstTag")
a.add newElement("childTag")
assert a.kind == xnElement
assert $a == """<firstTag>
<childTag />
</firstTag>""" Source Edit proc newXmlTree(tag: sink string; children: openArray[XmlNode];
attributes: XmlAttributes = nil): XmlNode {....raises: [],
tags: [], forbids: [].}Creates a new XML tree with tag, children and attributes.
See also:
Example:
var g = newElement("myTag")
g.add newText("some text")
g.add newComment("this is comment")
var h = newElement("secondTag")
h.add newEntity("some entity")
let att = {"key1": "first value", "key2": "second value"}.toXmlAttributes
let k = newXmlTree("treeTag", [g, h], att)
doAssert $k == """<treeTag key1="first value" key2="second value">
<myTag>some text<!-- this is comment --></myTag>
<secondTag>&some entity;</secondTag>
</treeTag>""" Source Edit proc replace(n: XmlNode; i: Natural; replacement: openArray[XmlNode]) {.
...raises: [], tags: [], forbids: [].}Replaces the i'th child of n with replacement openArray.
n must be of xnElement kind.
See also:
Example:
var f = newElement("myTag")
f.add newElement("first")
f.insert(newElement("second"), 0)
f.replace(0, @[newElement("third"), newElement("fourth")])
assert $f == """<myTag>
<third />
<fourth />
<first />
</myTag>""" Source Edit proc replace(n: XmlNode; slice: Slice[int]; replacement: openArray[XmlNode]) {.
...raises: [], tags: [], forbids: [].}Deletes the items n[slice] of n.
n must be of xnElement kind.
See also:
Example:
var f = newElement("myTag")
f.add newElement("first")
f.insert([newElement("second"), newElement("fifth")], 0)
f.replace(0..1, @[newElement("third"), newElement("fourth")])
assert $f == """<myTag>
<third />
<fourth />
<first />
</myTag>""" Source Edit proc tag(n: XmlNode): lent string {.inline, ...raises: [], tags: [], forbids: [].}Gets the tag name of n.
n has to be an xnElement node.
See also:
Example:
var a = newElement("firstTag")
a.add newElement("childTag")
assert $a == """<firstTag>
<childTag />
</firstTag>"""
assert a.tag == "firstTag" Source Edit proc tag=(n: XmlNode; tag: sink string) {.inline, ...raises: [], tags: [],
forbids: [].}Sets the tag name of n.
n has to be an xnElement node.
See also:
Example:
var a = newElement("firstTag")
a.add newElement("childTag")
assert $a == """<firstTag>
<childTag />
</firstTag>"""
a.tag = "newTag"
assert $a == """<newTag>
<childTag />
</newTag>""" Source Edit proc text(n: XmlNode): lent string {.inline, ...raises: [], tags: [], forbids: [].}Gets the associated text with the node n.
n can be a CDATA, Text, comment, or entity node.
See also:
Example:
var c = newComment("my comment")
assert $c == "<!-- my comment -->"
assert c.text == "my comment" Source Edit proc text=(n: XmlNode; text: sink string) {.inline, ...raises: [], tags: [],
forbids: [].}Sets the associated text with the node n.
n can be a CDATA, Text, comment, or entity node.
See also:
Example:
var e = newEntity("my entity")
assert $e == "&my entity;"
e.text = "a new entity text"
assert $e == "&a new entity text;" Source Edit proc toXmlAttributes(keyValuePairs: varargs[tuple[key, val: string]]): XmlAttributes {.
...raises: [], tags: [], forbids: [].}{key: value} pairs into XmlAttributes. Example:
let att = {"key1": "first value", "key2": "second value"}.toXmlAttributes
var j = newElement("myTag")
j.attrs = att
doAssert $j == """<myTag key1="first value" key2="second value" />""" Source Edit iterator items(n: XmlNode): XmlNode {.inline, ...raises: [], tags: [], forbids: [].}n. Example:
var g = newElement("myTag")
g.add newText("some text")
g.add newComment("this is comment")
var h = newElement("secondTag")
h.add newEntity("some entity")
g.add h
assert $g == "<myTag>some text<!-- this is comment --><secondTag>&some entity;</secondTag></myTag>"
# for x in g: # the same as `for x in items(g):`
# echo x
# some text
# <!-- this is comment -->
# <secondTag>&some entity;<![CDATA[some cdata]]></secondTag> Source Edit
© 2006–2024 Andreas Rumpf
Licensed under the MIT License.
https://nim-lang.org/docs/xmltree.html