user_78361084
user_78361084

Reputation: 3908

How do I keep the html tags when parsing xml?

I have the following xml I am trying to parse. My playground can be found 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 out the plain text and anything within html tags gets ignored. <strong>This has style</strong> gets ignored. How do I include that as well?

thx!

Upvotes: 3

Views: 52

Answers (1)

Ainar-G
Ainar-G

Reputation: 36199

Use innerxml tag:

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_

Upvotes: 4

Related Questions