我正在尝试解析以下xml。我的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)
}
}
这只会打印出纯文本,html标记内的任何内容都会被忽略。 <strong>This has style</strong>
被忽略了。我如何包括它?
THX!
答案 0 :(得分:4)
使用innerxml
代码:
type ResultSlice struct {
MyText []Result `xml:"results>result"`
}
type Result struct {
Text struct {
HTML string `xml:",innerxml"`
} `xml:"text"`
}