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

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

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 &quot;fmt&quot;
import &quot;encoding/xml&quot;

type ResultSlice struct {
	MyText []Result `xml:&quot;results&gt;result&quot;`
}
type Result struct {
	MyResult string `xml:&quot;text&quot;`
}

func main() {
	s := `&lt;myroot&gt;
	        &lt;results&gt;	          
	          &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;
	          &lt;result&gt;&lt;text&gt;No style here&lt;/text&gt;&lt;/result&gt;
	          &lt;result&gt;&lt;text&gt;Again, no style&lt;/text&gt;&lt;/result&gt;
		&lt;/results&gt;
	      &lt;/myroot&gt;`
	r := &amp;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. &lt;strong&gt;This has style&lt;/strong&gt; 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:&quot;results&gt;result&quot;`
}

type Result struct {
	Text struct {
		HTML string `xml:&quot;,innerxml&quot;`
	} `xml:&quot;text&quot;`
}

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:

确定