xml – Scala填充模板的方式?
发布时间:2020-12-15 23:57:04 所属栏目:百科 来源:网络整理
导读:在 Ruby中我可以这样: string=EOTEMPLATEroot hello to%s/to messagewelcome mr %s/message /hello .../rootEOTEMPLATE 当我想“渲染”模板时,我会这样做: rendered = string % ["me@mail.com","Anderson"] 它将使用数组中传递的值填充模板.有没有办法在Sc
在
Ruby中我可以这样:
string=<<EOTEMPLATE <root> <hello> <to>%s</to> <message>welcome mr %s</message> </hello> ... </root> EOTEMPLATE 当我想“渲染”模板时,我会这样做: rendered = string % ["me@mail.com","Anderson"] 它将使用数组中传递的值填充模板.有没有办法在Scala中执行此操作,而不是使用Java的String.format?如果我在Scala中写这个: val myStr = <root> <hello> <to>{address}</to> <message>{message}</message> </hello> </root> 生成的XML已经“填充”了.有没有办法可以“模板化”XML?
使用函数和Scala的XML:
val tmpl = {(address: String,message: String) => <root> <hello> <to>{address}</to> <message>{message}</message> </hello> </root> } 和: tmpl("me@mail.com","Anderson") 一些糖: def tmpl(f: Product => Elem) = new { def %(args: Product) = f(args) } val t = tmpl{case (address,message) => <root> <hello> <to>{address}</to> <message>{message}</message> </hello> </root> } t % ("me@mail.com","Anderson") (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |