A simple XML tree. More efficient and simpler than the DOM.
XmlNode = ref XmlNodeObj
XmlNode's. XmlNodeKind = enum xnText, ## a text element xnElement, ## an element with 0 or more children xnCData, ## a CDATA node xnEntity, ## an entity (like ``&thing;``) xnComment ## an XML comment
XmlNode's XmlAttributes = StringTableRef
xmlHeader = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"
proc newElement(tag: string): XmlNode {...}{.raises: [], tags: [].}PXmlNode of kind xnText with the given tag. proc newText(text: string): XmlNode {...}{.raises: [], tags: [].}PXmlNode of kind xnText with the text text. proc newComment(comment: string): XmlNode {...}{.raises: [], tags: [].}PXmlNode of kind xnComment with the text comment. proc newCData(cdata: string): XmlNode {...}{.raises: [], tags: [].}PXmlNode of kind xnComment with the text cdata. proc newEntity(entity: string): XmlNode {...}{.raises: [], tags: [].}PXmlNode of kind xnEntity with the text entity. proc text(n: XmlNode): string {...}{.inline, raises: [], tags: [].}proc text=(n: XmlNode; text: string) {...}{.inline, raises: [], tags: [].}proc rawText(n: XmlNode): string {...}{.inline, raises: [], tags: [].}proc rawTag(n: XmlNode): string {...}{.inline, raises: [], tags: [].}proc innerText(n: XmlNode): string {...}{.raises: [], tags: [].}proc tag(n: XmlNode): string {...}{.inline, raises: [], tags: [].}xnElement node. proc tag=(n: XmlNode; tag: string) {...}{.inline, raises: [], tags: [].}xnElement node. proc add(father, son: XmlNode) {...}{.inline, raises: [], tags: [].}proc insert(father, son: XmlNode; index: int) {...}{.inline, raises: [], tags: [].}proc len(n: XmlNode): int {...}{.inline, raises: [], tags: [].}proc kind(n: XmlNode): XmlNodeKind {...}{.inline, raises: [], tags: [].}proc `[]`(n: XmlNode; i: int): XmlNode {...}{.inline, raises: [], tags: [].}proc delete(n: XmlNode; i: Natural) {...}{.noSideEffect, raises: [], tags: [].}proc `[]`(n: var XmlNode; i: int): var XmlNode {...}{.inline, raises: [], tags: [].}proc attrs(n: XmlNode): XmlAttributes {...}{.inline, raises: [], tags: [].}proc attrs=(n: XmlNode; attr: XmlAttributes) {...}{.inline, raises: [], tags: [].}proc attrsLen(n: XmlNode): int {...}{.inline, raises: [], tags: [].}proc clientData(n: XmlNode): int {...}{.inline, raises: [], tags: [].}proc clientData=(n: XmlNode; data: int) {...}{.inline, raises: [], tags: [].}proc addEscaped(result: var string; s: string) {...}{.raises: [], tags: [].}result.add(escape(s)), but more efficient. proc escape(s: string): string {...}{.raises: [], tags: [].}| char | is converted to |
|---|---|
< |
< |
> |
> |
& |
& |
" |
" |
' |
' |
/ |
/ |
proc add(result: var string; n: XmlNode; indent = 0; indWidth = 2; addNewLines = true) {...}{.
raises: [], tags: [].}proc `$`(n: XmlNode): string {...}{.raises: [], tags: [].}<$xml ...$> declaration is produced, so that the produced XML fragments are composable. proc newXmlTree(tag: string; children: openArray[XmlNode];
attributes: XmlAttributes = nil): XmlNode {...}{.raises: [], tags: [].}proc child(n: XmlNode; name: string): XmlNode {...}{.raises: [], tags: [].}proc attr(n: XmlNode; name: string): string {...}{.raises: [], tags: [].}proc findAll(n: XmlNode; tag: string; result: var seq[XmlNode]) {...}{.raises: [], tags: [].}Iterates over all the children of n returning those matching tag.
Found nodes satisfying the condition will be appended to the result sequence, which can't be nil or the proc will crash. Usage example:
var html: XmlNode tags: seq[XmlNode] = @[] html = buildHtml() findAll(html, "img", tags) for imgTag in tags: process(imgTag)
proc findAll(n: XmlNode; tag: string): seq[XmlNode] {...}{.raises: [], tags: [].}var html: XmlNode
html = buildHtml(html)
for imgTag in html.findAll("img"):
process(imgTag) iterator items(n: XmlNode): XmlNode {...}{.inline, raises: [], tags: [].}iterator mitems(n: var XmlNode): var XmlNode {...}{.inline, raises: [], tags: [].}macro `<>`(x: untyped): untyped
<>a(href="http://nim-lang.org", newText("Nim rules."))
Produces an XML tree for:
<a href="http://nim-lang.org">Nim rules.</a>
© 2006–2018 Andreas Rumpf
Licensed under the MIT License.
https://nim-lang.org/docs/xmltree.html