英文:
How do I keep the html tags when parsing xml?
问题
我有以下的XML文件,我正在尝试解析它。我的playground在这里
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>
被忽略了。我如何包含它呢?
谢谢!
英文:
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!
答案1
得分: 4
使用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_
英文:
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_
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论