在XML中将XML文本作为参数编写

前端之家收集整理的这篇文章主要介绍了在XML中将XML文本作为参数编写前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我可以将变量作为多值参数传递:
scala> <b/>
res26: scala.xml.Elem = <b></b>

scala> Elem(null,"a",Null,TopScope,res26)
res27: scala.xml.Elem = <a><b></b></a>

但我无法将XML文本作为多值参数传递:

scala> Elem(null,<b/>)
<console>:12: error: not found: value <
Elem(null,<b/>)

但我可以将XML文本作为简单参数传递

scala> def bar(s:String,n:Elem) = s+n.toString
bar: (s: String,n: scala.xml.Elem)java.lang.String
scala> bar("super ",<a/>)
res30: java.lang.String = super <a></a>

谢谢

在XML元素之前添加空格使其工作:
scala> Elem(null,<b/>)
resN: scala.xml.Elem = <a><b></b></a>

Scala Language Specification,第1.5节:

In order to allow literal inclusion of XML fragments,lexical analysis switches from Scala mode to XML mode when encountering an opening angle bracket ’<’ in the following circumstance: The ’<’ must be preceded either by whitespace,an opening parenthesis or an opening brace and immediately followed by a character starting an XML name

原文链接:https://www.f2er.com/xml/292076.html

猜你在找的XML相关文章