I need to pair XMl into the Client model, so that in c.BirthDate.Cdata there was 1.01.2000
How can this be done?
Thanks to xml:",cdata" can be written to XML with CDATA, but it does not work back.

 xml := `<client><name>Вася</name><birth_date><![CDATA[1.01.2000]]></birth_date></client>` type Client struct { XMLName xml.Name `xml:"client"` Name string `xml:"name"` BirthDate *BirthDate `xml:"birth_date"` } type BirthDate struct { XMLName xml.Name `xml:"birth_date"` Cdata []byte `xml:",cdata"` Value []byte `xml:",chardata"` } var c Client xml.Unmarshal(xml, &c) 

    2 answers 2

    Why do you even BirthDate as a separate element? Here also works:

     type Client struct { XMLName xml.Name `xml:"client"` Name string `xml:"name"` BirthDate string `xml:"birth_date"` } 

    Playground: https://play.golang.org/p/P8591sArF2 .

    • The fact is that in the document with which I work the CDATA tag looks like this: & lt;! [CDATA [01/01/2000]] & gt; Now, I understood what was the matter, thanks! - Dmitry Matvienko

    My situation was this: the arrows were in the form of &lt; и &gt; &lt; и &gt; in this case, Unmarshal works incorrectly, does not parse CDATA

     xmlStr := `<client><name>Вася</name><birth_date>&lt;![CDATA[1.01.2000]]&gt;</birth_date></client>` 

    simple replacement helped

     b := bytes(xmlStr) b = bytes.Replace(b, []byte(`&lt;`), []byte("<"), -1) b = bytes.Replace(b, []byte(`&gt;`), []byte(">"), -1)