亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

func Encode(),編碼xml的所有部分

func Encode(),編碼xml的所有部分

Go
白豬掌柜的 2023-03-29 16:16:47
這是代碼:import ("bytes""encoding/xml""fmt")func main() {type body struct {    Message string `xml:"message"`}myXml := body{    Message:"This is <myText>",}    w := &bytes.Buffer{}        enc := xml.NewEncoder(w)        enc.Indent("", "  ")        if err := enc.Encode(myXml); err != nil {            panic(err)        }        request := w.String()        fmt.Println(request)    }有什么方法可以使字段消息的值不編碼。我不想編碼。這是結果:<body>  <message>This is &lt;myText&gt;</message></body>
查看完整描述

1 回答

?
心有法竹

TA貢獻1866條經驗 獲得超5個贊

轉義<>字符對于有效的 XML 是必須的。別擔心, 的內容<message>將是This is <myText>,但此文本必須出現在This is &lt;myText&gt;XML 源中。

請注意,使用xml:",innerxml"標記值,您可以強制輸出原始 XML 數據,如記錄在xml.Marshal()

- a field with tag ",innerxml" is written verbatim, not subject

? to the usual marshaling procedure.

像這樣:


type rawxml struct {

? ? Data string `xml:",innerxml"`

}

type body struct {

? ? Message rawxml `xml:"message"`

}

myXml := body{

? ? Message: rawxml{"This is <myText>"},

}

這將輸出(在Go Playground上嘗試):


<body>

? <message>This is <myText></message>

</body>

Or implementing and using custom xml.Marshaler, but again, this is invalid XML, this is not what you want. What you have right now is exactly what you want.


查看完整回答
反對 回復 2023-03-29
  • 1 回答
  • 0 關注
  • 97 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號