xml - 为什么 Scala XML 文字对标签之间的空格敏感?

标签 xml scala

我发现 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/

相关文章:

android - DOM 解析器教程

c# - C#读取XML文件

java - 使用 gm4java 在单个进程中进行多个图像操作

scala - 如何并行运行多个 sbt 项目?

android - 在 Android 中更改状态栏颜色

c# - 如何在 XSLT 中进行循环?

android - 在xml中设置样式

scala - 如何用akka-http将字节流解析为 `HttpRequest`对象?

scala - 如何使用 Apache Spark Scala 获取大型 CSV/RDD[Array[double]] 中所有列的直方图?

java - 无法创建文件/目标/流/编译/运行/$global/streams/outjava.io.IOException :Too many open files