<p>I have the following xml file:
<?xml version="1.0" encoding="utf-8" standalone="yes" ?></p>
<pre><code><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>
</code></pre>
<p>I tried to map it to a struct but it won't work for some odd reason:</p>
<pre><code>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"`
}
</code></pre>
<p>A link to a small playground file fetching the xml and trying to parse it:
<a href="https://play.golang.org/p/ZcWUt675JL" rel="nofollow">Playground</a></p>
<p>I have no idea why this isn't working. The xml is dead simple and still the mapping seems wrong. Any help is appreciated.</p>
<hr/>**评论:**<br/><br/>enmand: <pre><p>I think the issue you're encourtering is more with the Go Playground, than with your encoding structs.</p>
<blockquote>
<p>There are limitations to the programs that can be run in the playground:</p>
<p>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.</p>
</blockquote>
<p>I don't think you're able to to <code>http.Get()</code> in the sandbox. Loading the XML as <code>[]bytes</code> directly, and Unmarshalling seemed to work for me: <a href="https://play.golang.org/p/O9NBMfOCMb" rel="nofollow">https://play.golang.org/p/O9NBMfOCMb</a></p></pre>positivedown: <pre><p>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.</p></pre>enmand: <pre><p>Ah, I see on my local, the same issue. I looks like it's the XML version header:<code><?xml version="1.0" encoding="ISO-8859-1" standalone="yes" ?></code>. Looks like by default the <code>xml.Unmarshal</code> function doesn't work with that encoding. I found this SO article <a href="https://stackoverflow.com/questions/6002619/unmarshal-an-iso-8859-1-xml-input-in-go" rel="nofollow">https://stackoverflow.com/questions/6002619/unmarshal-an-iso-8859-1-xml-input-in-go</a> and this GitHub issue: <a href="https://github.com/golang/go/issues/8937" rel="nofollow">https://github.com/golang/go/issues/8937</a>. Hopefully they help :)</p></pre>positivedown: <pre><p>I missed that! Thank you!</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传