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?
Can you use antixml to create xml documents?
717 views Asked by schmmd At
2
There are 2 answers
2
On
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)
Yes, you can build (and serialize) XML documents:
You can also use Anti-XML's zippers to do some interesting editing tricks:
Now
newDoccontains the edited document:The
Zipperthatdoc \ "foo"returns is different from aNodeSeqin 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
converton anyscala.xml.Elem:I'd assumed the question was about programmatic creation.