user13964801
user13964801

Reputation:

Accessing all elements of xml in scala

I am working on a scala application. I have an Xml file as follows.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Response xmlns:ad3="http://something.something">
      <ad3:Company xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ad3:Type33">
            <ad3:Start xsi:type="ad3:Event">
                <ad3:value>3</ad3:value>
                <ad3:IsAvailable>false</ad3:IsAvailable>
                <ad3:Location>
                    <ad3:Level>High</ad3:Level>
                    <ad3:Id>1</ad3:Id>
                    <ad3:RollNumber>1</ad3:RollNumber>
                </ad3:Location>
                <ad3:TimeMilliseconds>11100</ad3:TimeMilliseconds>
            </ad3:Start>
            <ad3:End xsi:type="ad3:Event">
                <ad3:value>4</ad3:value>
                <ad3:IsAvailable>false</ad3:IsAvailable>
                <ad3:Location>
                    <ad3:Level>High</ad3:Level>
                    <ad3:Id>1</ad3:Id>
                    <ad3:RollNumber>1</ad3:RollNumber>
                </ad3:Location>
                <ad3:TimeMilliseconds>11100</ad3:TimeMilliseconds>
            </ad3:End>
            <ad3:CompanyMajor>
                <ad3:Level>High</ad3:Level>
                <ad3:Id>1</ad3:Id>
            </ad3:CompanyMajor>
            <ad3:IsHalfDayAvailable>false</ad3:IsHalfDayAvailable>
        </ad3:Company>
</Response>

I want to read this file. In my application I may need to access any of the tag at some point of time and may want to change it's value and save the data in xml object and sent that object to rabbitMq for further processing. How I can access each and every tag of this file and change values of it?

Upvotes: 0

Views: 680

Answers (1)

Matthias Berndt
Matthias Berndt

Reputation: 4577

The best way to process XML is usually to stay away from it entirely and treat it purely as a serialization format for a strongly-typed data model.

The way to do that in Scala is to compile the XML Schema file to Scala classes using scalaxb. Check out their documentation for details on how to set it up. If you're using sbt, I would highly recommend using sbt-scalaxb. It's documented how to do it exactly on their website, but the gist is:

  1. add the scalaxb plugin to project/plugins.sbt
  2. enable the plugin using enablePlugins(ScalaxbPlugin)
  3. add some necessary libraries:
libraryDependencies ++= Seq(
  "org.scala-lang.modules" %% "scala-xml" % "1.3.0",
  "org.scala-lang.modules" %% "scala-parser-combinators" % "1.1.2",
  "javax.xml.bind" % "jaxb-api" % "2.3.1"
)
  1. put the XML Schema file into src/main/xsc
  2. run the scalaxb task (though this will run automatically when you run the compile task.

You can then read an XML file using the scala standard xml library:

import scala.xml.XML
XML.loadFile("foo.xml")
// or from a string
XML.loadString("<foo />")

You can then use scalaxb.fromXML[generated.SomeType] to convert the XML to objects, perform whatever logic you need to, and then convert it back to XML using scalaxb.toXML.

Upvotes: 1

Related Questions