0

Given that i have a Node

var xml = XML.loadFile("some/file/here")

what can i do should i want to change the value of some element to a new value

That is,

...
<anElement>5</anElement>
...

to

...
<anElement>blooblahblahyah</anElement>
...    

Apologies if this is a pretty silly question, i'm incredibly new to Scala and haven't found any definitive answer on xml editing.

1 Answer 1

3

The Node is immutable and this makes editing it a bit tedious.
There are examples in the Scala XML book.

val foo = <foo><bar>1</bar><bar>2</bar></foo>
foo.copy (child = foo.child.map {case bar: scala.xml.Elem =>
  bar.copy (child = scala.xml.Text ((bar.text.toInt + 1).toString))})

res0: scala.xml.Elem = <foo><bar>2</bar><bar>3</bar></foo>

Sign up to request clarification or add additional context in comments.

1 Comment

Also take a look at xml.transform: scala-lang.org/api/current/… ; couple of examples: daily-scala.blogspot.ru/2009/12/xml-transformation-1.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.