如何使用scala.xml添加xml文档信息? [英] How do you add xml document info with scala.xml?
本文介绍了如何使用scala.xml添加xml文档信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
首先:
- 我知道反XML和可扩展性,但是我想使用标准的scala.xml
- 我更喜欢使用显式方法构建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>
,我也想获取XML标头,例如:
and I would like to get XML header too, like:
<?xml version="1.0"?>
<element>just a text</element>
问题:如何添加?
当然,这是一种常识性的方法,不是使用带有标题;-)的逐字逐字 println
进行的一些黑客行为.
Of course in common-sense way, not some hacking with extra verbatim println
with header ;-).
推荐答案
我发现的唯一解决方案是添加以下代码
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()
这篇关于如何使用scala.xml添加xml文档信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文