Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active April 2, 2023 10:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dacr/41c95c5485d327f75ea172be77afa737 to your computer and use it in GitHub Desktop.
Save dacr/41c95c5485d327f75ea172be77afa737 to your computer and use it in GitHub Desktop.
simple scala xml operations / published by https://github.com/dacr/code-examples-manager #8f13bd90-9553-4225-b756-d708b4c7b6d0/36375888710163c4cd4101fdf81b2e9c5d32a6ed
// summary : simple scala xml operations
// keywords : scala, xml, @testable
// publish : gist
// authors : David Crosson
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2)
// id : 8f13bd90-9553-4225-b756-d708b4c7b6d0
// created-on : 2020-08-25T10:53:15Z
// managed-by : https://github.com/dacr/code-examples-manager
// run-with : scala-cli $file
// ---------------------
//> using scala "3.1.1"
//> using dep "org.scalatest::scalatest:3.2.10"
//> using dep "org.scala-lang.modules::scala-xml:2.0.1"
// ---------------------
import org.scalatest._, flatspec._, matchers._
import scala.xml._
object XMLBasicsTest extends AnyFlatSpec with should.Matchers {
override def suiteName = "XMLBasicsTest"
"scala.xml library" should "allow simple XML syntax" in {
val sample = {
<that>
<truc>
<bidule prop1="val1" prop2="val2">
{1+1}
</bidule>
</truc>
</that>
}
(sample \ "truc" \ "bidule" \@ "prop1") shouldBe "val1"
(sample \\ "bidule" \@ "prop1") shouldBe "val1"
(sample \\ "bidule").text.trim shouldBe "2"
}
it should "be possible to parse an xml string" in {
val sample = """
|<that>
| <truc>
| <bidule prop1="val1" prop2="val2">
| </bidule>
| </truc>
|</that>""".stripMargin
val parsed = XML.loadString(sample)
(parsed \ "truc" \ "bidule" \@ "prop1") shouldBe "val1"
(parsed \\ "bidule" \@ "prop1") shouldBe "val1"
}
it should "be possible to serialize to a string" in {
val msg = "hello world"
val sample = <truc><bidule><machin msg={msg}></machin></bidule></truc>
sample.toString() shouldBe """<truc><bidule><machin msg="hello world"></machin></bidule></truc>"""
}
}
XMLBasicsTest.execute()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment