当解析 XML 时,如何保留 HTML 标签?

huangapple go评论118阅读模式
英文:

How do I keep the html tags when parsing xml?

问题

我有以下的XML文件,我正在尝试解析它。我的playground在这里

  1. package main
  2. import "fmt"
  3. import "encoding/xml"
  4. type ResultSlice struct {
  5. MyText []Result `xml:"results>result"`
  6. }
  7. type Result struct {
  8. MyResult string `xml:"text"`
  9. }
  10. func main() {
  11. s := `<myroot>
  12. <results>
  13. <result><text><strong>This has style</strong>Then some not-style</text></result>
  14. <result><text>No style here</text></result>
  15. <result><text>Again, no style</text></result>
  16. </results>
  17. </myroot>`
  18. r := &ResultSlice{}
  19. if err := xml.Unmarshal([]byte(s), r); err == nil {
  20. fmt.Println(r)
  21. } else {
  22. fmt.Println(err)
  23. }
  24. }

这将只打印出纯文本,任何在HTML标签内的内容都会被忽略。<strong>This has style</strong>被忽略了。我如何包含它呢?

谢谢!

英文:

I have the following xml I am trying to parse. My playground can be found here

  1. package main
  2. import &quot;fmt&quot;
  3. import &quot;encoding/xml&quot;
  4. type ResultSlice struct {
  5. MyText []Result `xml:&quot;results&gt;result&quot;`
  6. }
  7. type Result struct {
  8. MyResult string `xml:&quot;text&quot;`
  9. }
  10. func main() {
  11. s := `&lt;myroot&gt;
  12. &lt;results&gt;
  13. &lt;result&gt;&lt;text&gt;&lt;strong&gt;This has style&lt;/strong&gt;Then some not-style&lt;/text&gt;&lt;/result&gt;
  14. &lt;result&gt;&lt;text&gt;No style here&lt;/text&gt;&lt;/result&gt;
  15. &lt;result&gt;&lt;text&gt;Again, no style&lt;/text&gt;&lt;/result&gt;
  16. &lt;/results&gt;
  17. &lt;/myroot&gt;`
  18. r := &amp;ResultSlice{}
  19. if err := xml.Unmarshal([]byte(s), r); err == nil {
  20. fmt.Println(r)
  21. } else {
  22. fmt.Println(err)
  23. }
  24. }

This will only print out the plain text and anything within html tags gets ignored. &lt;strong&gt;This has style&lt;/strong&gt; gets ignored. How do I include that as well?

thx!

答案1

得分: 4

使用innerxml标签:

  1. type ResultSlice struct {
  2. MyText []Result `xml:"results>result"`
  3. }
  4. type Result struct {
  5. Text struct {
  6. HTML string `xml:",innerxml"`
  7. } `xml:"text"`
  8. }

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

英文:

Use innerxml tag:

  1. type ResultSlice struct {
  2. MyText []Result `xml:&quot;results&gt;result&quot;`
  3. }
  4. type Result struct {
  5. Text struct {
  6. HTML string `xml:&quot;,innerxml&quot;`
  7. } `xml:&quot;text&quot;`
  8. }

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

huangapple
  • 本文由 发表于 2014年11月30日 05:06:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/27207119.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定