Last active
November 6, 2017 20:41
-
-
Save maxant/6f86feea7553b05291543febf33bdfe4 to your computer and use it in GitHub Desktop.
Scala version of read_xml_out_csv.py
This file contains hidden or 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
/* | |
* built using Scala 2.12.4 in intellij | |
* prerequisites: add library for org.scala-lang.modules:scala-xml_2.12:1.0.6 | |
*/ | |
package ch.maxant.readxmloutcsv | |
import java.io.{File, PrintWriter} | |
import scala.xml.XML | |
import java.lang.System.{currentTimeMillis => now} | |
import scala.collection.mutable | |
object Main { | |
def main(args: Array[String]): Unit = { | |
val start = now | |
val model = new mutable.HashMap[String, mutable.HashMap[String, mutable.HashMap[String, String]]]() | |
var lastParamName: String = null | |
// read all files, collecting data | |
new File(".") | |
.listFiles | |
.filter(_.isFile) | |
.filter(_.getName.endsWith(".xml")) | |
.flatMap{file => | |
println("parsing file " + file) | |
val fileModel = new mutable.HashMap[String, mutable.HashMap[String, String]]() | |
model.put(file.getName, fileModel) | |
val xml = XML.loadFile(file) | |
xml \\ "servlet" map((fileModel, _)) | |
}.flatMap{case (fileModel, servlet) => | |
val servletModel = new mutable.HashMap[String, String]() | |
fileModel.put((servlet \\ "name").text, servletModel) | |
servlet.child.map((servletModel, _)) | |
}.foreach { case (servletModel, child) => | |
child.label match { | |
case "param-name" => lastParamName = child.text | |
case "param-value" => servletModel.put(lastParamName, child.text) | |
case _ => //do nothing | |
} | |
} | |
val uniqueSortedParamNames = model.flatMap(_._2.values).flatMap(_.keys).toList.distinct.sorted | |
// build header | |
var lines = "filename,servlet name," + uniqueSortedParamNames.mkString(",") + "\n" | |
// build content | |
lines += model.map( file => | |
file._2.toList.sortBy(_._1).map( servlet => | |
file._1 + "," + servlet._1 + "," + uniqueSortedParamNames.map(servlet._2.getOrElse(_, "")).mkString(",") | |
).mkString("\n") | |
).mkString("\n") | |
// write file | |
new PrintWriter("output.csv") { try { write(lines) } finally close() } | |
println("Wrote file in %dms".format(now-start)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment