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: failure to Unmarshal the XML text string #29117

Closed
conero opened this Issue Dec 6, 2018 · 3 comments

Comments

Projects
None yet
2 participants
@conero
Copy link

conero commented Dec 6, 2018

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

$ go version
go version go1.11.1 windows/amd64

Does this issue reproduce with the latest release?

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

go env Output
$ go env
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\Administrator\AppData\Local\go-build
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=C:\Users\Administrator\go
set GOPROXY=
set GORACE=
set GOROOT=C:\Go
set GOTMPDIR=
set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
set GCCGO=gccgo
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\ADMINI~1\AppData\Local\Temp\go-build455657050=/tmp/go-build -gno-record-gcc-switches

What did you do?

When I use xml.Unmarshal to act the XML text string, the result does work. And one of these is ok, my script like this:
look the source, and test scriptTestBridge_Info is ok.
source

/*
<?xml version="1.0" encoding="UTF-8"?>
<log>
   <logentry revision="4907">
      <author>joshua</author>
      <date>2018-12-04T03:52:28.702087Z</date>
      <msg></msg>
   </logentry>
   <logentry revision="4904">
      <author>conero</author>
      <date>2018-12-04T03:01:11.280212Z</date>
      <msg></msg>
   </logentry>
</log>
*/

type XLEnter struct {
	XMLName  xml.Name `xml:"logentry"`
	Revision string   `xml:"revision,attr"`
	Author   string   `xml:"author"`
	Date     string   `xml:"date"`
	Msg      string   `xml:"msg"`
}

// log --xml 
type XmlLog struct {
	XMLName xml.Name `xml:"log"`
	Enter   []XLEnter
}

// svn log --xml
func (b *Bridge) Log(pArgs ...string) (XmlLog, error) {
	args := b.GetArgs("log", "--xml")
	
	if pArgs != nil && len(pArgs) > 1 {
		args = append(args, pArgs...)
	}
	//fmt.Println(args)
	out, err := Call(args...)
	fmt.Println(out, err)
	var dd XmlLog
	if err != nil {
		return dd, err
	}

	err = xml.Unmarshal([]byte(out), &dd)
	if err != nil {
		return dd, err
	}
	return dd, nil
}

use testing

func TestBridge_Log(t *testing.T) {
	brd := &Bridge{Path: _testPath}
	dd, er := brd.Log("-r", "4900:4903")
	if er != nil {
		fmt.Println(" Error: " + er.Error())
		return
	}
	fmt.Println(dd)
	fmt.Println(dd.Enter)
	for _, d := range dd.Enter {
		s := `{"revision": "` + d.Revision + `", "author": "` + d.Author + `", "date": "` + d.Date + `", "msg": "` + d.Msg + `"}`
		fmt.Println(" " + s)
		//fmt.Println(d)
	}
}

What did you expect to see?

{{ log} []}
[]

What did you see instead?

@bcmills bcmills changed the title xml/Unmarshal: failure to Unmarshal the XML text string encoding/xml: failure to Unmarshal the XML text string Dec 10, 2018

@bcmills

This comment has been minimized.

Copy link
Member

bcmills commented Dec 10, 2018

Please complete the issue template. What did you see instead?

Ideally, provide the example as a runnable Playground link.

@conero

This comment has been minimized.

Copy link
Author

conero commented Dec 11, 2018

 {"revision": "4900", "author": "yanghua", "date": "2018-12-04T02:38:09.577087Z", "msg": "it test msg-m12"},
 ...
 {"revision": "4903", "author": "dzj", "date": "2018-12-04T02:55:23.952087Z", "msg": "it test msg-m19"}
{{ log} []}
[]
@bcmills

This comment has been minimized.

Copy link
Member

bcmills commented Dec 11, 2018

The encoding/xml package can only manipulate exported fields. Your example did not export the entry field, and that field was missing an xml: annotation.

With those issues corrected (https://play.golang.org/p/V10Vk-FUuq-), it seems to work as expected.

@bcmills bcmills closed this Dec 11, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.