greenoldman
greenoldman

Reputation: 21082

How do you add xml document info with scala.xml?

First of all:

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 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 ;-).

Upvotes: 10

Views: 2004

Answers (1)

Nikolay Ivanov
Nikolay Ivanov

Reputation: 8935

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()

Upvotes: 10

Related Questions