there are a few examples for using Anti-Xml to extract information from XML documents, but none that I could find of using Anti-Xml to create XML documents. Does Anti-Xml support creating documents, or should I use another library for this (which one?). Does anyone have an example of creating an XML document with Anti-Xml?
-
1Actually, I did ask myself the very same question some time ago, because I was unsatisfied with the lack of namespace support with Scala XML literals. So I looked for the alternative libraries to build my XML. For my use case, the answer seems to be negative, judging by Travis' answer. – ziggystar May 04 '12 at 09:10
2 Answers
Yes, you can build (and serialize) XML documents:
import com.codecommit.antixml._
val doc = Elem(None, "doc", Attributes(), Map(), Group(
Elem(None, "foo", Attributes("id" -> "bar"), Map(), Group(Text("baz")))
))
val writer = new java.io.StringWriter
val serializer = new XMLSerializer("UTF-8", true)
serializer.serializeDocument(doc, writer)
You can also use Anti-XML's zippers to do some interesting editing tricks:
val foos = doc \ "foo"
val newFoo = foo.head.copy(children = Group(Text("new text!")))
val newDoc = foos.updated(0, newFoo).unselect
Now newDoc
contains the edited document:
scala> newDoc.toString
res1: String = <doc><foo id="bar">new text!</foo></doc>
The Zipper
that doc \ "foo"
returns is different from a NodeSeq
in that it carries information about its context, which allows you to "undo" the selection operation done by \
.
Update in response to ziggystar's comment below: if you want something like Scala's XML literals, you can just use convert
on any scala.xml.Elem
:
val test: com.codecommit.antixml.Elem = <test></test>.convert
I'd assumed the question was about programmatic creation.

- 1
- 1

- 138,631
- 12
- 375
- 680
-
1Well, I think the question should be read as "Is there a way to use Anti-Xml to build documents *in a convenient way*?". Like is there some kind of DSL for creating documents? Of course you can manually glue together implementation classes... – ziggystar May 03 '12 at 13:14
-
@ziggystar: See my update—you can just use Scala's literals and convert. – Travis Brown May 03 '12 at 14:04
Unless you are using other parts of anti-xml I'd stick with Scala's literals. However if you are looking for a better alternative with a nice builder DSL, in place updates, XPath usage (DSL and String based) etc, then I'd suggest a look at Scales. From the 0.3-RC5 release site (http://scala-scales.googlecode.com/svn/sites/scales/scales-xml_2.9.1/0.3-RC5/index.html):
val ns = Namespace("test:uri")
val nsa = Namespace("test:uri:attribs")
val nsp = nsa.prefixed("pre")
val builder =
ns("Elem") /@ (nsa("pre", "attr1") -> "val1",
"attr2" -> "val2",
nsp("attr3") -> "val3") /(
ns("Child"),
"Mixed Content",
ns("Child2") /( ns("Subchild") ~> "text" )
)
Also for direct serialisation you can leverage streaming, as Scales Xml model unifies the model for streaming and tree based processing.
I'm short before a 0.3-RC6 with many more docs (and very cool xml comparison framework) but I'm waiting on migrating to sonatype first. (eta next week)

- 1,240
- 7
- 8
-
Do you know if there exists a XML library that somehow integrates Scala's XML literals? I think with macros in 2.10 this might become quite possible. – ziggystar May 04 '12 at 09:13
-
I'd be tempted to look at it, but I currently support 2.8.x, 2.9.x and I'd like to support 2.10.x uniformly. Adding macro support to re-use the literals would then have to be another lib (doesn't mean I won't do it though, just to see if its possible). As per your comment above though, the full namespace support is lacking, Scales targets namespaces directly, you simply can't have an unqualified name at any usage site. (Asynchronous parsing is next on my list however). – Chris May 05 '12 at 09:50