[plt-scheme] Help with redirecting ports

From: Ryan Culpepper (ryanc at ccs.neu.edu)
Date: Tue Nov 4 18:19:34 EST 2008

stig wrote:
> I am trying to read a content.xml from an openoffice .odt file. using
> unzip-entry from the zip.plt library. Which works fine, I can pump the
> output into ssax:xml->sxml but I can't seem to redirect the output to
> a file my latest attempt (below) only dumps the sxml output onto the
> screen in DrScheme. Any help would be much appreciated ( I commented
> out the copy-port because it also wasn't doing what I wanted)
> 
> (require (planet "unzip.ss" ("dherman" "zip.plt" 2)))
> (require (planet "sxml.ss" ("lizorkin" "sxml.plt" 2 0)))
> (require (planet "ssax.ss" ("lizorkin" "ssax.plt" 2 0)))
> 
> (require (lib "port.ss"))
> 
> (define (odt2sxml filename)
>   (unzip-entry
>      filename
>      (read-zip-directory filename )
>       #"content.xml"
>       (lambda (name dir in)
>           (with-output-to-file "local-copy.sxml"
>               (lambda ()
>                 (ssax:xml->sxml in (quote ()))
>                 ;(copy-port in (current-output-port) )
>                 ))))

The 'with-output-to-file' procedure only redirects the thunk's *output*, 
not its result. You need to insert a call to 'write'. Here's how I would 
do it:

;; read-odt-as-sxml : path -> SXML
(define (read-odt-as-sxml filename)
   (unzip-entry
    filename
    (read-zip-directory filename)
    #"content.xml"
    (lambda (name dir in)
      (ssax:xml->sxml in (quote ())))))

;; convert-odt-to-sxml : path path -> void
(define (convert-odt-to-sxml in-file out-file)
   (with-output-to-file out-file
     (lambda ()
       (write (read-odt-as-sxml in-file)))))

Ryan

> _________________________________________________
>   For list-related administrative tasks:
>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme



Posted on the users mailing list.