Hello, I am very new to Nim and recently discovered it. I thought it might be cool to generate an XML file for my app cast updater. I seem to misunderstand or there is bug when I am trying to nest with indentation.
Format exmple:
# <xml header>
# <rss>
# <channel>
# <title>
# <description>
# <item>
# <title><title/>
# <sparkle><sparkle/>
# <pub date><pub date/>
# <enclosure/>
# </item>
# </channel>
# </rss>
Code example:
# item tree ########################################################################################
var t = newElement("title")
t.add newText("Version")
var sr = newElement("sparkle:releaseNotesLink")
sr.add newText("some url to the release notes")
var pd = newElement("pubDate")
pd.add newText("the date")
var enc = newElement("enclosure")
let repo = "https://my-repo/"
let sV = "1.0"
let eAttr = {"url": repo, "sparkle:version": sV}.toXmlAttributes
enc.attrs = eAttr
let itemTree = newXmlTree("item", [t, sr, pd, enc])
# channel tree #####################################################################################
var title = newElement("title")
title.add newText("My App Title")
var description = newElement("description")
description.add newText("For GUI updates")
let channelTree = newXmlTree("channel", [title, description, itemTree])
# rss tree #########################################################################################
let namespace = "http://www.andymatuschak.org/xml-namespaces/sparkle"
let elements = "http://purl.org/dc/elements/1.1/"
let v = "2.0"
let nsAtt = {"xmlns:sparkle": namespace, "xmlns:dc": elements, "version": v}.toXmlAttributes
let rssTree = newXmlTree("rss", [channelTree], nsAtt)
# output to file ###################################################################################
echo rssTree
let f = open("test.xml", fmWrite)
try:
write(f, xmlHeader & $rssTree)
finally:
f.close()
Output:
<rss version="2.0" xmlns:sparkle="http://www.andymatuschak.org/xml-namespaces/sparkle" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel>
<title>My App Title</title>
<description>For GUI updates</description>
<item>
<title>Version</title>
<sparkle:releaseNotesLink>some url to the release notes</sparkle:releaseNotesLink>
<pubDate>the date</pubDate>
<enclosure sparkle:version="1.0" url="https://my-repo/" />
</item>
</channel></rss>
I am expecting the
<channel>
element to be on its own line indented. I am also expecting the end </rss>
to be on its own line as well.
I am not sure where I am going wrong. If anyone has any tips it would be greatly appreciated. Thank you kindly!
It's probably because you are using newXmlTree for both channel and rss tags. May be you should use newElement for channel too?
PS: I am just guessing; I haven't used this library.
I have attempted to fix this issue in this PR: https://github.com/nim-lang/Nim/pull/13482/files
XML/xmltree experts: Please review that PR and help me make it better!