可以用antixml来创建xml文档吗? [英] Can you use antixml to create xml documents?
问题描述
有一些使用 Anti-Xml 从 XML 文档中提取信息的示例,但我找不到使用 Anti-Xml 创建 XML 文档的示例.Anti-Xml 是否支持创建文档,或者我应该为此使用另一个库(哪个?).有没有人有用 Anti-Xml 创建 XML 文档的例子?
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?
推荐答案
是的,您可以构建(和序列化)XML 文档:
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)
您还可以使用 Anti-XML 的 zippers 来做一些有趣的编辑技巧:>
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
现在 newDoc
包含编辑过的文档:
Now newDoc
contains the edited document:
scala> newDoc.toString
res1: String = <doc><foo id="bar">new text!</foo></doc>
Zipper
doc \ "foo"
返回的不同于 NodeSeq
因为它携带有关其上下文的信息,这允许您撤消"由\
完成的选择操作.
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 \
.
更新以响应 ziggystar 在下面的评论:如果你想要像 Scala 的 XML 文字这样的东西,你可以使用convert
在任何 scala.xml.Elem
上:
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.
这篇关于可以用antixml来创建xml文档吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!