如何使用scala.xml添加xml文档信息?

时间:2022-05-22 19:36:17

First of all:

首先:

  • I am aware of anti-xml, and scales, but I would like to use standard scala.xml
  • 我知道反xml和scale,但我想使用标准的scala.xml
  • I prefer to build xml document using explicit methods, not with implicit xml syntax built into Scala
  • 我更喜欢使用显式方法构建xml文档,而不是使用Scala内置的隐式xml语法

Ok, so I have such piece of code:

好的,所以我有这样一段代码:

val text = new scala.xml.Text("just a text")
val root = new scala.xml.Elem(null,"element",null,scala.xml.TopScope,text)
val doc = new scala.xml.Document()
doc.docElem = root
println(doc.toString())

Almost good but as result I get:

几乎不错,但结果我得到:

<element>just a text</element>

and I would like to get XML header too, like:

我也想得到XML标题,如:

<?xml version="1.0"?>
<element>just a text</element>

Question: How to add it?

问题:如何添加?

Of course in common-sense way, not some hacking with extra verbatim println with header ;-).

当然,从常识的方面来说,不是一些带有标题的额外逐字打印的黑客;-)。

1 个解决方案

#1


8  

The only solution I've found is to add the following code

我发现的唯一解决方案是添加以下代码

val writer : PrintWriter = new PrintWriter(System.out)
XML.write(writer,root,"utf-8",true,null)
writer.flush()

#1


8  

The only solution I've found is to add the following code

我发现的唯一解决方案是添加以下代码

val writer : PrintWriter = new PrintWriter(System.out)
XML.write(writer,root,"utf-8",true,null)
writer.flush()