This page is no longer maintained — Please continue to the home page at www.scala-lang.org

XML.write doesn't output anything

2 replies
Sciss
Joined: 2008-12-17,
User offline. Last seen 28 weeks 5 days ago.

hi,

i need to use XML.write (as my interface uses an OutputStream and not a File i cannot use XML.save), but i only get empty results:

$ scala
Welcome to Scala version 2.8.0.final (Java HotSpot(TM) Client VM, Java 1.6.0_20).
Type in expressions to have them evaluated.
Type :help for more information.

scala> import java.io._; import xml._
import java.io._
import xml._

scala> val os = new FileOutputStream( new File( "/tmp/test.xml" ))
os: java.io.FileOutputStream = java.io.FileOutputStream@132a4df

scala> val node =
node: scala.xml.Elem =

scala> val w = new OutputStreamWriter( os )
w: java.io.OutputStreamWriter = java.io.OutputStreamWriter@169674

scala> XML.write( w, node, "UTF-8", true, null )

scala> os.close

scala> :quit
$ ls -la /tmp/test.xml
-rw-r--r-- 1 rutz wheel 0 17 Jul 18:03 /tmp/test.xml

what's going on there?

thanks for help!

best, -sciss-

Sciss
Joined: 2008-12-17,
User offline. Last seen 28 weeks 5 days ago.
Re: XML.write doesn't output anything

sorry, forget about it. i need to add

w.flush

after XML.write...

best, -sciss-

Am 17.07.2010 um 18:05 schrieb Sciss:

> hi,
>
> i need to use XML.write (as my interface uses an OutputStream and not a File i cannot use XML.save), but i only get empty results:
>
> $ scala
> Welcome to Scala version 2.8.0.final (Java HotSpot(TM) Client VM, Java 1.6.0_20).
> Type in expressions to have them evaluated.
> Type :help for more information.
>
> scala> import java.io._; import xml._
> import java.io._
> import xml._
>
> scala> val os = new FileOutputStream( new File( "/tmp/test.xml" ))
> os: java.io.FileOutputStream = java.io.FileOutputStream@132a4df
>
> scala> val node =
> node: scala.xml.Elem =
>
> scala> val w = new OutputStreamWriter( os )
> w: java.io.OutputStreamWriter = java.io.OutputStreamWriter@169674
>
> scala> XML.write( w, node, "UTF-8", true, null )
>
> scala> os.close
>
> scala> :quit
> $ ls -la /tmp/test.xml
> -rw-r--r-- 1 rutz wheel 0 17 Jul 18:03 /tmp/test.xml
>
>
> what's going on there?
>
> thanks for help!
>
> best, -sciss-
>

Randall R Schulz
Joined: 2008-12-16,
User offline. Last seen 1 year 29 weeks ago.
Re: XML.write doesn't output anything

On Saturday July 17 2010, Sciss wrote:
> ...
>
> $ scala
> Welcome to Scala version 2.8.0.final (Java HotSpot(TM) Client VM,
> Java 1.6.0_20). Type in expressions to have them evaluated.
> Type :help for more information.
>
> scala> import java.io._; import xml._
> import java.io._
> import xml._
>
> scala> val os = new FileOutputStream( new File( "/tmp/test.xml" ))
> os: java.io.FileOutputStream = java.io.FileOutputStream@132a4df
>
> scala> val node =
> node: scala.xml.Elem =
>
> scala> val w = new OutputStreamWriter( os )
> w: java.io.OutputStreamWriter = java.io.OutputStreamWriter@169674
>
> scala> XML.write( w, node, "UTF-8", true, null )
>
> scala> os.close
>
> scala> :quit
> $ ls -la /tmp/test.xml
> -rw-r--r-- 1 rutz wheel 0 17 Jul 18:03 /tmp/test.xml
>
>
> what's going on there?

You're writing to "w" but closing "os", which is downstream from w. If
you close w instead, it writes the XML to the file.

> thanks for help!
>
> best, -sciss-

Randall Schulz

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland