Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parsing CDATA does not work #11

Open
i124q2n8 opened this issue Aug 25, 2020 · 0 comments
Open

Parsing CDATA does not work #11

i124q2n8 opened this issue Aug 25, 2020 · 0 comments

Comments

@i124q2n8
Copy link

i124q2n8 commented Aug 25, 2020

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

e.Text = string(bytes.TrimSpace(token))

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)
	}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant