For example, if the xml is simple:
<p>this is a test xml</p>
Then I can use struct like this:
type Dummy struct { XMLName xml.Name `xml:"p"` Value string `xml:",chardata"` }
But if the xml has special character, like
<p>this is a test < xml & </p>
I used the same struct and it caused error, and it seems nothing to do with struct, because I use interface{} and it caused error the same.
I read the document of package "encoding/xml" but cannot find the right way.
Does anyone know how to do this?
This XML is not well-formed. It contain syntax error because character &
has a special meaning. If you need to insert if as a symbol itself change it with &
. After parsing it will be replaced back to usual &
.
The same rule applies to <
- it should be replaced with <
.
<p>this is a test <gt; xml & </p>
This is the general rule for all XML parsers and encoders. Any other XML system awaits similar behavior.
If you look at the error (try it on golang playground), it's telling you the input is not valid XML.
XML syntax error on line 1: expected element name after <
The text is not allowed to contain plain < or & like that, see the Character data section of the xml spec