CDATA go xml CDATA
所有 XML 文档中的文本均会被解析器解析。
只有 CDATA 区段(CDATA section)中的文本会被解析器忽略。
go xml CDATA
type ToUserNameS struct {
XMLName xml.Name `xml:"ToUserName"`
Text string `xml:",cdata"`
}
type FromUserNameS struct {
XMLName xml.Name `xml:"FromUserName"`
Text string `xml:",cdata"`
}
type MsgTypeS struct {
XMLName xml.Name `xml:"MsgType"`
Text string `xml:",cdata"`
}
type ContentS struct {
XMLName xml.Name `xml:"Content"`
Text string `xml:",cdata"`
}
type Receiving_standard_messages struct {
XMLName xml.Name `xml:"xml"`
ToUserName *ToUserNameS `xml:"ToUserName"`
FromUserName *FromUserNameS `xml:"FromUserName"`
CreateTime string `xml:"CreateTime"`
MsgType *MsgTypeS `xml:"MsgType"`
Content *ContentS `xml:"Content"`
MsgId string `xml:"MsgId"`
MsgDataId string `xml:"MsgDataId"`
}
type Passive_user_reply_message struct {
XMLName xml.Name `xml:"xml"`
ToUserName *ToUserNameS `xml:"ToUserName"`
FromUserName *FromUserNameS `xml:"FromUserName"`
CreateTime string `xml:"CreateTime"`
MsgType *MsgTypeS `xml:"MsgType"`
Content *ContentS `xml:"Content"`
}
bs, err := io.ReadAll(ctx.Request().Body)
if err != nil {
sreCli.Log.Error(err)
}
sreCli.Log.Debug(string(bs))
t := Receiving_standard_messages{}
err = xml.Unmarshal(bs, &t)
if err != nil {
sreCli.Log.Error(err)
}
sreCli.Log.Debug(t.FromUserName.Text, t.Content.Text)
ts := func() string {
return utils.Int2Str(int(time.Now().Unix()))
}()
respX := Passive_user_reply_message{}
_ToUserName := &ToUserNameS{}
_ToUserName.Text = t.FromUserName.Text
respX.ToUserName = _ToUserName
_FromUserName := &FromUserNameS{}
_FromUserName.Text = t.ToUserName.Text
respX.FromUserName = _FromUserName
respX.FromUserName.Text = t.ToUserName.Text
respX.CreateTime = ts
_MsgType := &MsgTypeS{}
_MsgType.Text = "text"
respX.MsgType = _MsgType
_Content := &ContentS{}
_Content.Text = respTxt(t.Content.Text)
respX.Content = _Content
respXMLStr, err := xml.Marshal(respX)
if err != nil {
sreCli.Log.Error(err)
}
sreCli.Log.Debug(string(respXMLStr))