Scala:使用回调以编程方式创建 xml?
我有一个库,它使用类回调来运行树结构。 一种简化的变体是:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用 JATL,它看起来会像这样
If you use JATL, it would look something like