-1

I have XML like this:

<a>
  <b name = "d"value = "1"/>
  <b name = "e"value = "2"/>
  <b name = "f"value = "3"/>
</a>

I want to fetch the value where name is e. How can i do that in scala using XML library of scala (import scala.xml.XML)

I am trying something like this:

map{line=>{
  val rec = line.toString.split("\\^")
  var duration = XML.loadString(rec(4))
  ((duration\\"a"\\"b"\@"name").toString())


}}

But getting blank as output

Kumar Harsh
  • 423
  • 5
  • 26

1 Answers1

1

You can use spark-xml if you are using spark. Add the dependency to your project in Maven or Sbt

Now you can directly read xml as a dataframe and perform an operation

import org.apache.spark.sql.SparkSession
import org.apache.spark.sql.functions._

  val df = spark.sqlContext.read
    .format("com.databricks.spark.xml")
    .option("rowTag", "a")
      .option("valueTag", "bvalue")
    .load("/home/sk/IdeaProjects/TestProjectForDemo/src/main/scala/test.xml")


  df.withColumn("b", explode($"b")).where($"b._name" === "e")
    .show(false)

Output:

+----------+
|b         |
+----------+
|[e,2,null]|
+----------+

Hope this helps!

koiralo
  • 22,594
  • 6
  • 51
  • 72