I have the following struct:
type XMLProduct struct {
XMLName xml.Name `xml:"row"`
ProductId string `xml:"product_id"`
ProductName string `xml:"product_name"`
OriginalPrice string `xml:"original_price"`
BargainPrice string `xml:"bargain_price"`
TotalReviewCount int `xml:"total_review_count"`
AverageScore float64 `xml:"average_score"`
}
And I use the encoding/xml
to encode this and then display it on web page.
The ProductName
field needs to be enclosed with <![CDATA[]]
. But if I write it as <![CDATA[ + p.ProductName + ]]>
, the <
and >
will be translated to <
and >
.
How can I create the CDATA
at minimal cost?
encoding/xml/marshal.go
does not suggest that outputting CDATA is supported. (Again, CDATA is technically unnecessary. Maybe the specification can be modified after all?) – Serology