liudanking
liudanking

Reputation: 121

Go xml.Marshal returns invalid characters

I use the code below generate a XML encoding of string str:

str := string([]byte{0x01})
marshalBytes, _ := xml.Marshal(str)
fmt.Println(string(marshalBytes)) // output: <string>�</string>; � is [239 191 189] in bytes.

Obviously, � is not equivalent of 0x01.

How can I fix it?

Upvotes: 1

Views: 401

Answers (1)

Thundercat
Thundercat

Reputation: 120931

The bytes [239 191 189] are the UTF-8 encoding of the Unicode Replacement Character.

The XML marshaler replaces the byte 0x1 with the Unicode Replacement Character because the byte 0x01 is not a legal character in XML.

It is not possible to prevent the XML marshaler from using the replacement.

Upvotes: 5

Related Questions