I figured out a solution:

https://play.golang.org/p/sWR1hAumYh

I created a type that wraps string and implements the xml.Marshaler 
interface.  Then I just cast my strings to that type.

Hey go team, any chance this could be made the default behavior for 
xml.CharData types so a single cast would do the job?


On Tuesday, October 11, 2016 at 10:00:51 AM UTC-7, sm...@brillig.org wrote:
>
> Sorry, I should have been more clear.  The reason I used a slice was 
> because I need an arbitrary number of elements. So I can't just use a 
> static struct with the chardata tags.
>
> On Tuesday, October 11, 2016 at 4:49:34 AM UTC-7, Konstantin Khomoutov 
> wrote:
>>
>> On Mon, 10 Oct 2016 22:10:37 -0700 (PDT) 
>> Scott <gr8w...@gmail.com> wrote: 
>>
>> > I'm trying to Marshal XML where an element has mixed content: 
>> > https://www.w3.org/TR/REC-xml/#sec-mixed-content 
>> > 
>> > I tried just using []interface{} but if I put in just a string, 
>> > Marshal surrounds each string with the name of the slice: 
>> > 
>> > https://play.golang.org/p/erh3mQmrZD 
>> > 
>> > I'm trying to get the output to be: 
>> >   <root> 
>> >       <element1>foo</element1> 
>> >       hello 
>> >       <element2>bar</element2> 
>> >       world 
>> >   </root> 
>> > 
>> > Any ideas? 
>>
>> Yes.  "\nhello\n" and "\nworld\n" are what is called "character data" 
>> in XML parlance.  So if you go with the standard approach to marshaling 
>> data to XML -- via a struct type with properly annotated fields -- 
>> you should annotate the fields for your character data chunks with the 
>> ",chardata" modifiers. 
>>
>> See the docs on encoding/xml.Marshal function for more info. 
>>
>> Here's a working example: 
>>
>> --------8<-------- 
>>    package main 
>>     
>>    import ( 
>>        "bytes" 
>>        "encoding/xml" 
>>        "fmt" 
>>    ) 
>>     
>>    type E struct { 
>>        XMLName struct{} `xml:"root"` 
>>        E1      string   `xml:"element1"` 
>>        A       string   `xml:",chardata"` 
>>        E2      string   `xml:"element2"` 
>>        Z       string   `xml:",chardata"` 
>>    } 
>>     
>>    func main() { 
>>        e := E{ 
>>            E1: "foo", 
>>            A:  "hello", 
>>            E2: "bar", 
>>            Z:  "world", 
>>        } 
>>     
>>        var b bytes.Buffer 
>>        enc := xml.NewEncoder(&b) 
>>        enc.Indent("", "\t") 
>>     
>>        err := enc.Encode(&e) 
>>        if err != nil { 
>>            panic(err) 
>>        } 
>>        err = enc.Flush() 
>>        if err != nil { 
>>            panic(err) 
>>        } 
>>     
>>        fmt.Println(b.String()) 
>>    } 
>> --------8<-------- 
>>
>> Playground link: <https://play.golang.org/p/pWaYmOT675> 
>>
>> Please note that whitespace is only insignificant in XML where *you* 
>> think it is (that is, there's no inherent semantics of it in the XML 
>> spec.  So you should be aware that in your example your character data 
>> chunks are not "hello" and "world" but rather 
>>
>>   LF SP SP SP SP "hello" LF 
>>
>> and 
>>
>>   LF SP SP SP SP "world" LF 
>>
>> , respectively (provided linebreaks are sole LFs) 
>>
>> So if you really want those bits of whitespace to be present in the 
>> resulting XML document you have to make sure you embed them to your 
>> fields annotated with ",chardata". 
>>
>> Hope this helps. 
>>
>

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