Can you use antixml to create xml documents?

2020-07-07 11:26发布

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?

2条回答
Emotional °昔
2楼-- · 2020-07-07 11:40

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.

查看更多
可以哭但决不认输i
3楼-- · 2020-07-07 11:54

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)

查看更多
登录 后发表回答