Skip to content

Instantly share code, notes, and snippets.

@fancellu
Created April 24, 2014 14:52
Show Gist options
  • Save fancellu/745bb780adf938c39395 to your computer and use it in GitHub Desktop.
Save fancellu/745bb780adf938c39395 to your computer and use it in GitHub Desktop.
object StaX extends App {
import scala.io.Source
import java.io.File
import javax.xml.stream._
val inputFactory = XMLInputFactory.newInstance();
//val src = Source.fromFile(new File("e:/downloads/medsamp2014.xml"),"UTF-8")
val src = Source.fromFile(new File("e:/downloads/enwiki-20140402-abstract.xml"),"UTF-8")
val er = inputFactory.createXMLEventReader(src.reader)
var count=0;
while (er.hasNext&&count<10){
val ev=er.nextEvent()
count+=1
println(s"$count:\t$ev")
}
}
@fancellu
Copy link
Author

You can make it even more Scala friendly thus:

implicit class XMLEventIterator(ev:XMLEventReader) extends scala.collection.Iterator[XMLEvent]{
def hasNext=ev.hasNext
def next=ev.nextEvent()
}

er.dropWhile(!_.isStartElement()).take(10).zipWithIndex.foreach{case (ev,idx)=>println(s"${idx+1}:\t$ev")}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment