Scala:使用回调以编程方式创建 xml?

发布于 2024-12-15 15:27:33 字数 624 浏览 0 评论 0原文

我有一个库,它使用类回调来运行树结构。 一种简化的变体是:

class CallBack {
    openGroup(name:String)
    closeGroup(name:String)
    item(name:String)
}

openGroup 一步步进入树,closeGroup 步出树,而 item 是叶子。 我想要的是创建一个简单的 xml 结构 当我运行代码时。一个天真的解决方案是简单地 做类似下面的伪代码的事情:

class CallBack {
    var xml:String = ""
    openGroup(name:String)  = { xml += "<ol label=" + name + ">"}
    closeGroup(name:String) = { xml += "</ol>" }
    item(name:String)       = { xml += "<li name=.... />" }
}

我想更好的解决方案是使用 Node:s、Groups 等来创建它, 或其他相关技术,然后创建 xml。

您知道如何以某种体面的方式从上面创建 xml 吗?

I have a library that runs through a tree structure using callbacks into a class.
A simplified variant is:

class CallBack {
    openGroup(name:String)
    closeGroup(name:String)
    item(name:String)
}

openGroup thus steps one step into the tree, closeGroup steps out and item are leafs.
What I would like to to is to create a simple xml structure
as I run through the code. A naive solution would be to simply
do something like the below pseudocode:

class CallBack {
    var xml:String = ""
    openGroup(name:String)  = { xml += "<ol label=" + name + ">"}
    closeGroup(name:String) = { xml += "</ol>" }
    item(name:String)       = { xml += "<li name=.... />" }
}

I guess a nicer solution would be to create this using Node:s, Groups etc.,
or other relevant technique and then create the xml.

Do you have any idea how to create xml from the above in some decent manner?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

太傻旳人生 2024-12-22 15:27:33

如果您使用 JATL,它看起来会像这样

class CallBack(writer: Writer) {
    private val html = new Html(writer)
    def openGroup(name:String) = html.ol.label(name)
    def closeGroup(name:String) = html.end
    def item(name:String) = html.li.label(name).end
}

If you use JATL, it would look something like

class CallBack(writer: Writer) {
    private val html = new Html(writer)
    def openGroup(name:String) = html.ol.label(name)
    def closeGroup(name:String) = html.end
    def item(name:String) = html.li.label(name).end
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文