3

I've been surprised by how non-intuitive the standard Scala XML library is (beyond defining in-line XML and basic path tree traversal) as well as the lack of any apparent replacements. It looks like at one point anti-xml was gaining traction but it appears that development has been stale for some time.

Specifically I'm looking to manipulate the XML tree by adding and removing sub-trees, something along the lines of:

val tree1 = <root><foo>foo</foo></root>
val tree2 = <root><bar>bar</bar></root>
tree1 + tree2 == <root><foo>foo</foo><bar>bar</bar></root>

Pimping the existing Scala XML library is certainly an option but if there are existing libraries that afford this functionality I'd prefer to let them do the heavy lifting.

Do folks have recommendations on appropriate XML libraries for Scala in 2015 (i.e. >= 2.10) where this sort of manipulation is possible and straightforward?

eharik
  • 155
  • 1
  • 8
  • Well, if in-line XML isn't enough to wow you, know that you can also use any Java XML library (and there are quite a few). – Paul Draper Feb 07 '15 at 05:20
  • Thanks, @PaulDraper, this is true. While Java integration is helpful it usually comes with it's own set of challenges, especially when trying to stay idiomatically accurate. The use of scala.collection.JavaConversions goes a long way to obviate the problem but my preference would be to use a pure Scala implementation if one exists. I don't mean to short-change the accomplishments of the Scala team on their XML implementation. In-lining XML is pretty darn slick. But tree manipulations start to get hairy real quick. – eharik Feb 07 '15 at 13:46
  • FWIW [this](http://stackoverflow.com/questions/2199040/scala-xml-building-adding-children-to-existing-nodes) is the approach I'm currently running with. – eharik Feb 07 '15 at 16:37

1 Answers1

2

There's a more current fork of anti-xml that can be found here. Version 0.5.2 works with Scala 2.11.

Using anti-xml, your use case can be implemented with:

import com.codecommit.antixml._

val tree1 = <root><foo>foo</foo></root>.convert
val tree2 = <root><bar>bar</bar></root>.convert

tree1.addChildren(tree2.children)
// <root><foo>foo</foo><bar>bar</bar></root>
ncreep
  • 473
  • 3
  • 11