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

encoding/xml: can't bind attr val to a field of struct with flag #53623

Closed
speauty opened this issue Jun 30, 2022 · 0 comments
Closed

encoding/xml: can't bind attr val to a field of struct with flag #53623

speauty opened this issue Jun 30, 2022 · 0 comments

Comments

@speauty
Copy link

speauty commented Jun 30, 2022

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

$ go version 
go version go1.18.1 linux/amd64

Does this issue reproduce with the latest release?

maybe not?

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

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/speauty/.cache/go-build"
GOENV="/home/speauty/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/speauty/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/speauty/go"
GOPRIVATE=""
GOPROXY="https://goproxy.cn"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.18.1"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build1154348162=/tmp/go-build -gno-record-gcc-switches"

What did you do?

I want to parse a xml data from a xml file to a related struct by using encoding/xml lib.

What did you expect to see?

that struct has all data parsed from xml, including attr.

What did you see instead?

struct is right, but the field binding to pointed attr flag is empty, no data with that.

the xml file data like follow:

<ProducData_ExpXML>
    <IronInfo OrderNo="220629192669300" />
</ProducData_ExpXML>

and code, emmm

type XmlExp struct {
	XMLName  xml.Name `xml:"ProducData_ExpXML"`
	IronInfo IronInfo
}

type IronInfo struct {
	OrderNo string `xml:"OrderNo.attr"`
}

func (controller Common) UploadXml(ctx *gin.Context) { // gin web handle
	tmpXML, _ := ctx.FormFile("xml")
	xmlParsed := &XmlExp{}
	open, err := tmpXML.Open()
	defer func() { _ = open.Close() }()
	if err != nil {
		fmt.Println("file open", err)
	}
	data, _ := io.ReadAll(open)
	err = xml.Unmarshal(data, xmlParsed)
	if err != nil {
		fmt.Println("xml unmarshal", err)
	}
	resp := (&http.JsonResponse{}).Default()
	resp.SetData(map[string]interface{}{
		"xml_parsed": xmlParsed,
	})
	controller.Response(ctx, resp, nil) 
	return
}

of course, response is:

{
    "c": 200,
    "m": "ok",
    "d": {
        "xml_parsed": {
            "XMLName": {
                "Space": "",
                "Local": "ProducData_ExpXML"
            },
            "IronInfo": {
                "OrderNo": ""
            }
        }
    }
}

the go-play link is: https://go.dev/play/p/bc2tww7J4wQ, which just simplified the above code.

@speauty speauty changed the title affected/package: encoding/xml encoding/xml: can't bind attr val to a field of struct with flag Jun 30, 2022
@speauty speauty closed this as completed Jun 30, 2022
@golang golang locked and limited conversation to collaborators Jun 30, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants