Problem parsing xml file

blov · · 445 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I have the following xml file: &lt;?xml version=&#34;1.0&#34; encoding=&#34;utf-8&#34; standalone=&#34;yes&#34; ?&gt;</p> <pre><code>&lt;java-update-map version=&#34;1.0&#34;&gt; &lt;mapping&gt; &lt;version&gt;1.8.0_51&lt;/version&gt; &lt;url&gt;https://javadl-esd-secure.oracle.com/update/1.8.0/au-descriptor-1.8.0_101-b13.xml&lt;/url&gt; &lt;/mapping&gt; &lt;mapping&gt; &lt;version&gt;1.8.0_60&lt;/version&gt; &lt;url&gt;https://javadl-esd-secure.oracle.com/update/1.8.0/au-descriptor-1.8.0_101-b13.xml&lt;/url&gt; &lt;/mapping&gt; &lt;/java-update-map&gt; </code></pre> <p>I tried to map it to a struct but it won&#39;t work for some odd reason:</p> <pre><code>type Java_Update_Map struct { XMLName xml.Name `xml:&#34;java-update-map&#34;` Maps []Mapping `xml:&#34;mapping&#34;` } type Mapping struct { Url string `xml:&#34;url&#34;` Version string `xml:&#34;version&#34;` } </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&#39;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&#39;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&#39;t think you&#39;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 &amp;{{} []}. 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&#39;s the XML version header:<code>&lt;?xml version=&#34;1.0&#34; encoding=&#34;ISO-8859-1&#34; standalone=&#34;yes&#34; ?&gt;</code>. Looks like by default the <code>xml.Unmarshal</code> function doesn&#39;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

445 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传