为什么 Scala XML 文字对标签之间的空格敏感?
我发现 Scala XML 文字对空格很敏感,这有点奇怪,不是吗?因为 XML 解析器通常不会关心标签之间的空格。
这很糟糕,因为我想在代码中整齐地列出 XML:
<sample>
<hello />
</sample>
但是 Scala 认为这与
<sample><hello /></sample>
Proof is in the pudding 的值不同:
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
...给出了什么?
I've discovered that Scala XML literals are sensitive to whitespace, which is kinda strange, isn't it? since XML parsers don't normally give a damn about spaces between the tags.
This is a bummer because I'd like to set out my XML neatly in my code:
<sample>
<hello />
</sample>
but Scala considers this to be a different value to
<sample><hello /></sample>
Proof is in the pudding:
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
... What gives?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果你喜欢它,你应该对它进行修剪:
If you liked it you should have put a trim on it:
如果要将 XML 文字转换为
StringBuilder
:If you want to convert the XML literal to a
StringBuilder
: