如何使用 Scala 将 xml 文档解析为流? [英] How do I parse an xml document as a stream using Scala?
问题描述
如何使用 Scala 将 xml 文档解析为流?我已经使用 java 中的 Stax API 来完成此操作,但我想知道是否有scala"方式来执行此操作.
How do I parse an xml document as a stream using Scala? I've used the Stax API in java to accomplish this, but I'd like to know if there is a "scala" way to do this.
推荐答案
使用包 scala.xml.pull.摘自 Scaladoc for Scala 2.8 的片段:
Use package scala.xml.pull. Snippet taken from the Scaladoc for Scala 2.8:
import scala.xml.pull._
import scala.io.Source
object reader {
val src = Source.fromString("<hello><world/></hello>")
val er = new XMLEventReader(src)
def main(args: Array[String]) {
while (er.hasNext)
Console.println(er.next)
}
}
您可以在 er
上调用 toIterator
或 toStream
以获得真正的 Iterator
或 Stream代码>.
You can call toIterator
or toStream
on er
to get a true Iterator
or Stream
.
这是 2.7 版本,略有不同.但是,与 Scala 2.8 不同,测试它似乎表明它没有检测到流的结尾.
And here's the 2.7 version, which is slightly different. However, testing it seems to indicate it doesn't detect the end of the stream, unlike in Scala 2.8.
import scala.xml.pull._
import scala.io.Source
object reader {
val src = Source.fromString("<hello><world/></hello>")
val er = new XMLEventReader().initialize(src)
def main(args: Array[String]) {
while (er.hasNext)
Console.println(er.next)
}
}
这篇关于如何使用 Scala 将 xml 文档解析为流?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!