I'm totally new to scala. I want to serialize scala objects to xml using the java xstream library. (Is this a good idea or is there a better way?)
How to install the library so that i can import:
import com.thoughtworks.xstream.io.{HierarchicalStreamReader, HierarchicalStreamWriter}
now thoughtworks is not defined.
Is there a url to add in the build.sbt? or is there a location to copy the xstreams.jar?
To add to @GamingFelix answer about scala-xml, SBT allows to easily include Java libraries into your Scala project.
When adding libraryDependencies
to your project, you would usually write something like this:
libraryDependencies ++= "com.somecompany" %% "someproject" % "1.0.0"
This will attempt to resolve the following Maven project:
com.somecompany:someproject_2.12:1.0.0
Note the _2.12
!
To import a Java project, you have to instead write:
libraryDependencies ++= "com.somecompany" % "someproject" % "1.0.0"
Note the single %
instead of the double %%
!
Now, SBT will try to resolve the Maven project:
com.somecompany:someproject:1.0.0
Which is the one you are looking for.
So, in your case write:
libraryDependencies += "com.thoughtworks.xstream" % "xstream" % "1.4.11.1"
When searching on Maven, you will actually be given such import statements right away:
https://search.maven.org/artifact/com.thoughtworks.xstream/xstream/1.4.11.1/jar
There is actually very good support for xml in the standard scala library.
I think you can read more about it here:
https://github.com/scala/scala-xml
All you need to do is to add the import in your class
import scala.xml
If you really want to use the java library instead. You can probably import it by adding the dependency into your build.sbt
.
(Edit to include this part of the answer by ygor)
Check out mvnrepository.com/artifact/com.thoughtworks.xstream/xstream/…
There is a "SBT" tab with the line, which you need to add to build.sbt
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With