go-xmldom icon indicating copy to clipboard operation
go-xmldom copied to clipboard

Parsing CDATA does not work

Open i124q2n8 opened this issue 4 years ago • 0 comments

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)
	}
}

i124q2n8 avatar Aug 25 '20 16:08 i124q2n8