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.
Definition. XML parsing is the process of reading an XML document and providing an interface to the user application for accessing the document. An XML parser is a software apparatus that accomplishes such tasks.
Scala makes processing and producing XML very easy. This enables programmers to very easily talk to web services and other sources of XML data.
Python allows parsing these XML documents using two modules namely, the xml. etree. ElementTree module and Minidom (Minimal DOM Implementation).
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)
}
}
You can call toIterator
or toStream
on er
to get a true Iterator
or Stream
.
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)
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With