I have the following xml file: <?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<java-update-map version="1.0">
<mapping>
<version>1.8.0_51</version>
<url>https://javadl-esd-secure.oracle.com/update/1.8.0/au-descriptor-1.8.0_101-b13.xml</url>
</mapping>
<mapping>
<version>1.8.0_60</version>
<url>https://javadl-esd-secure.oracle.com/update/1.8.0/au-descriptor-1.8.0_101-b13.xml</url>
</mapping>
</java-update-map>
I tried to map it to a struct but it won't work for some odd reason:
type Java_Update_Map struct {
XMLName xml.Name `xml:"java-update-map"`
Maps []Mapping `xml:"mapping"`
}
type Mapping struct {
Url string `xml:"url"`
Version string `xml:"version"`
}
A link to a small playground file fetching the xml and trying to parse it: Playground
I have no idea why this isn't working. The xml is dead simple and still the mapping seems wrong. Any help is appreciated.
评论:
enmand:
positivedown:I think the issue you're encourtering is more with the Go Playground, than with your encoding structs.
There are limitations to the programs that can be run in the playground:
The playground can use most of the standard library, with some exceptions. The only communication a playground program has to the outside world is by writing to standard output and standard error.
I don't think you're able to to
http.Get()
in the sandbox. Loading the XML as[]bytes
directly, and Unmarshalling seemed to work for me: https://play.golang.org/p/O9NBMfOCMb
enmand:I tested it locally one my computer. The playground link was just so people know what code i run. If I run the code on my computer the return value is &{{} []}. The struct is empty for some reason.
positivedown:Ah, I see on my local, the same issue. I looks like it's the XML version header:
. Looks like by default the
xml.Unmarshal
function doesn't work with that encoding. I found this SO article https://stackoverflow.com/questions/6002619/unmarshal-an-iso-8859-1-xml-input-in-go and this GitHub issue: https://github.com/golang/go/issues/8937. Hopefully they help :)
I missed that! Thank you!
