Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I parse an xml document as a stream using Scala?

Tags:

xml

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.

like image 955
ScArcher2 Avatar asked May 25 '10 21:05

ScArcher2


People also ask

What is parsing an XML?

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.

What is Scala XML?

Scala makes processing and producing XML very easy. This enables programmers to very easily talk to web services and other sources of XML data.

Which module can you use to parse an XML file using Python?

Python allows parsing these XML documents using two modules namely, the xml. etree. ElementTree module and Minidom (Minimal DOM Implementation).


1 Answers

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)
  }
}
like image 51
Daniel C. Sobral Avatar answered Oct 09 '22 02:10

Daniel C. Sobral