Question

Suppose I have an Anti-XML object, e.g.:

import com.codecommit.antixml._
val child = <child attr="val">...</child>.convert

I want to construct an XML object that contains child as a child:

<parent foo="bar"><foo/><child attr="val">...</child><foo/></parent>

The obvious way would be

val parent : Elem = <parent foo="bar"><foo/>{ child }<foo/></parent>.convert

The problem is that Scala's XML literals don't recognize Anti-XML's objects, so child gets converted to a string, and embedded in parent as a text node:

<parent foo="bar"><foo/>&lt;child attr="val"&gt;...&lt;/child&gt;<foo/></parent>

How can I work around this issue?

Was it helpful?

Solution

XML.loadString should do the trick:

val child: com.codecommit.antixml.Elem = <child />.convert
val parent: scala.xml.Elem = <parent>{scala.xml.XML.loadString(child.toString)}</parent>

For more complicated scenarios of deep manipulation, I think you would do better by avoiding using literals and using the built in Zipper support.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top