我发现 Scala XML 文字对空格很敏感,这有点奇怪,不是吗?因为 XML 解析器通常不会在意标签之间的空格。
这很糟糕,因为我想在我的代码中整齐地设置我的 XML:
<sample>
<hello />
</sample>
但是 Scala 认为这是一个不同的值
<sample><hello /></sample>
证据就在布丁里:
scala> val xml1 = <sample><hello /></sample>
xml1: scala.xml.Elem = <sample><hello></hello></sample>
scala> val xml2 = <sample>
| <hello />
| </sample>
xml2: scala.xml.Elem =
<sample>
<hello></hello>
</sample>
scala> xml1 == <sample><hello /></sample>
res0: Boolean = true
scala> xml1 == xml2
res1: Boolean = false
... 给出了什么?
最佳答案
如果你喜欢它,你应该把它修剪一下:
scala> val xml1 = <sample><hello /></sample>
xml1: scala.xml.Elem = <sample><hello></hello></sample>
scala> val xml2 = <sample>
| <hello />
| </sample>
xml2: scala.xml.Elem =
<sample>
<hello></hello>
</sample>
scala> xml1 == xml2
res14: Boolean = false
scala> xml.Utility.trim(xml1) == xml.Utility.trim(xml2)
res15: Boolean = true
关于xml - 为什么 Scala XML 文字对标签之间的空格敏感?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4636077/