使用 Scala 中的属性创建 xml

Create xml with attributes in scala

如何为 scala.xml

中的元素设置属性值,例如值

这行不通:(

def getXml(fooValue: String, barValue: String): Node = 
    val fooBar = <foo bar="{barValue}">
       { fooValue }
    </foo>

你必须不带引号:<foo bar={barValue}>

这样就可以了:

定义:

def createXMLElement(value: String, attributeValue: String) : Node =   
<foo attribute={attributeValue}>{value}</foo>

例子

scala> createXMLElement("Hello World", "boring")
res2: scala.xml.Node = <foo attribute="boring">Hello World</foo>

在给出的示例中,assign 结果为 val 并期望 return 类型 Node。 return分配的类型是Unit