-
-
Save fancellu/745bb780adf938c39395 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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")}