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

encoding/xml: no way to output cdata field into a nested node #16198

Open
akrylysov opened this Issue Jun 27, 2016 · 1 comment

Comments

Projects
None yet
3 participants
@akrylysov
Copy link

akrylysov commented Jun 27, 2016

What version of Go are you using (go version)?

go version go1.6.2 darwin/amd64

What operating system and processor architecture are you using (go env)?

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/akrylysov/go"
GORACE=""
GOROOT="/usr/local/Cellar/go/1.6.2/libexec"
GOTOOLDIR="/usr/local/Cellar/go/1.6.2/libexec/pkg/tool/darwin_amd64"
GO15VENDOREXPERIMENT="1"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fno-common"
CXX="clang++"
CGO_ENABLED="1"

What did you do?

type Test struct {
    Name string `xml:"Person>Name,cdata"`
}

https://play.golang.org/p/aVay4sw8xA

What did you expect to see?

<Test><Person><Name><![CDATA[foo]]</Name></Person></Test>

What did you see instead?

error: xml: invalid tag in field Name of type main.Test: "Person>Name,cdata"

I'm working on a web service which supports JSON and XML as output formats:

type Response struct {
    XMLName xml.Name `json:"-" xml:"response"`
    Foo string       `json:"foo" xml:"foo"`
    Bar string       `json:"bar" xml:"bar"`
}

It worked perfectly, but recently we got a new requirement from our external partner to wrap all XML values into CDATA. So instead of:

<response><foo>foo1</foo><bar>bar2</bar></response>

I need this:

<response><foo><![CDATA[foo1]]</foo><bar><![CDATA[bar2]]</bar></response>

Currently Go doesn't provide a way to do that without introducing a new nested structure like:

type CdataString struct {
    Value string `xml:",cdata"`
}

type Response struct {
    XMLName xml.Name `json:"-" xml:"response"`
    Foo CdataString  `json:"foo" xml:"foo"`
    Bar CdataString  `json:"bar" xml:"bar"`
}

which breaks JSON serialization.

@akrylysov akrylysov changed the title encoding/xml: no way to output cdata fierld into a nested node encoding/xml: no way to output cdata field into a nested node Jun 27, 2016

@ianlancetaylor ianlancetaylor added this to the Unplanned milestone Jun 27, 2016

crholm pushed a commit to crholm/go that referenced this issue Oct 25, 2018

Rasmus Holm
encoding/xml: allow cdata/chardata to be named and nested with annota…
…tions

A fix that will allow for annotating cdata and chardata with
names and nested elements. e.g. `a-name,cdata` or `a>b>c,cdata`.
A typical usecase might be marshling to json and xml is requierd
and reducing the need for mapping the sturct to others.

Fixes golang#16198

crholm pushed a commit to crholm/go that referenced this issue Oct 25, 2018

Rasmus Holm
encoding/xml: allow cdata/chardata to be named and nested with annota…
…tions

A fix that will allow for annotating cdata and chardata with
names and nested elements. e.g. `a-name,cdata` or `a>b>c,cdata`.
A typical usecase might be marshling to json and xml is requierd
and reducing the need for mapping the sturct to others.

Fixes golang#16198
@gopherbot

This comment has been minimized.

Copy link

gopherbot commented Oct 25, 2018

Change https://golang.org/cl/144518 mentions this issue: encoding/xml: allow cdata/chardata to be named and nested with annotations

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment