How to keep html tags while parsing xml?

I have the following xml which I am trying to parse. Find my site here

package main

import "fmt"
import "encoding/xml"

type ResultSlice struct {
    MyText []Result `xml:"results>result"`
}
type Result struct {
    MyResult string `xml:"text"`
}

func main() {
    s := `<myroot>
            <results>             
              <result><text><strong>This has style</strong>Then some not-style</text></result>
              <result><text>No style here</text></result>
              <result><text>Again, no style</text></result>
        </results>
          </myroot>`
    r := &ResultSlice{}
    if err := xml.Unmarshal([]byte(s), r); err == nil {
        fmt.Println(r)
    } else {
        fmt.Println(err)
    }

}

      

This will only print plain text and anything in the html tags is ignored. <strong>This has style</strong>

ignored. How do I enable this as well?

THH!

+3


source to share


1 answer


Use tag innerxml

:

type ResultSlice struct {
    MyText []Result `xml:"results>result"`
}

type Result struct {
    Text struct {
        HTML string `xml:",innerxml"`
    } `xml:"text"`
}

      



Playground: http://play.golang.org/p/U8SIUIvOC_

+4


source







All Articles