That makes sense, and in fact my XML output with 
s gets parsed later 
correctly. My personal use case has input XML files with literal LF 
characters, and it's frustrating that when my Go program modifies that 
file, they're all converted, which makes my git commit including that XML 
file messy. After your explanation though I've decided that's just me being 
petty.

I've also found a solution though! Implement io.PipeWriter such that all 
instances of 
 are replaced with LF.

type MyWriter struct {
    File *os.File
}
func (w MyWriter) Close() error { return w.File.Close() }
func (w MyWriter) CloseWithError(err error) error { return nil }
func (w MyWriter) Write(data []byte) (n int, err error) {
    n = len(data)
    data = bytes.Replace(data, []byte("
"), []byte("\n"), -1)
    _, err = w.File.Write(data)
    return
}

then later...
f, _ = os.Create(...)
w = MyWriter{File:f}
encoder := xml.NewEncoder(w)
_ = encoder.Encode(...)

Thanks for the response!

Patrick

On Tuesday, August 15, 2017 at 11:53:24 AM UTC-7, Lutz Horn wrote:
>
> Hi, 
>
> Am 15.08.17 um 08:31 schrieb patrick...@gmail.com <javascript:>: 
> > I simply want the XML files my program outputs to not have newline 
> > characters escaped into &#xA;. My input XML files contain newlines and I 
> > want to preserve them. 
>
> The &#xa; is the XML entity for LF. This is correct XML which contains 
> exactly what you put into it: 
>
> <Content><Elt>line 1&#xA;line 2</Elt></Content> 
>
> Any XML parser will be able to handle the LF. For example, xmllint does: 
>
> > $ xmllint -format content.xml 
> > <?xml version="1.0"?> 
> > <Content> 
> >   <Elt>line 1 
> > line 2</Elt> 
> > </Content> 
>
> So there is no but that needs to be fixed. 
> xml.NewEncoder(os.Stdout).Encode() works as expected and produces output 
> that is valid. 
>
> Lutz 
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to