2

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?

4

1 回答 1

3

XML.loadString应该做的伎俩:

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

Zipper对于更复杂的深度操作场景,我认为避免使用文字和使用内置支持会做得更好。

于 2012-06-25T22:22:35.230 回答