out put with cdata
package main //warning: go version must >=1.6 import ( "encoding/xml" "fmt" //"runtime" ) type Test struct { Name CdataString `xml:"Person>Name"` Vast string `xml:"vast,attr"` Ad string `xml:""` } type CdataString struct { Value string `xml:",cdata"` } func main() { //fmt.Println(runtime.Version()) //name := CdataString{Value: "bbbccc" } v := &Test{ Name: CdataString{Value: "bbbccc"}, } output, err := xml.MarshalIndent(v, " ", " ") if err != nil { fmt.Printf("error: %v\n", err) } fmt.Println(string(output)) }
output result:
<Test vast=""> <Person> <Name><![CDATA[bbbccc]]></Name> </Person> <Ad></Ad> </Test>
有疑问加站长微信联系(非本文作者)