Can you use antixml to create xml documents?

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?

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.


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)


Need Your Help

How can I safely loop until there is nothing more to do without using a “placeholder” while conditon?

c# asp.net-web-api while-loop resharper httpwebresponse

In order to call my Web API method until no more data is returned (I'm fetching it in batches, to keep each result set small, due to the 98-lb-weakling persona of the client (Windows CE handheld

Why does this not dot the line correctly?

ios core-graphics

CGContextRef currentContext = UIGraphicsGetCurrentContext();

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.