go-xmldom
go-xmldom copied to clipboard
Parsing CDATA does not work
I was parsing a XML file that contains CData sections and got empty strings. After a little digging I found the error. It seems that the underlying xml
package produces multiple xml.CharData
-Tokens for a node if it contains CData.
Replacing
https://github.com/subchen/go-xmldom/blob/e1029cd9087cb9c9c8941b155414b8b8e7ce293a/dom.go#L69
with
e.Text += string(bytes.TrimSpace(token))
fixes the error.
Additionally I wrote a test case for this issue.
func TestParseCData(t *testing.T) {
doc, err := xmldom.ParseXML(`
<r>
<![CDATA[
Hello World!
]]>
</r>
`)
if err != nil {
t.Fail()
}
if doc.Root.Text != "Hello World!" {
t.Errorf(`got "%s", expected "Hello World!"`, doc.Root.Text)
}
}