Problem parsing xml file

blov · 2016-09-20 11:00:03 · 594 次点击    
这是一个分享于 2016-09-20 11:00:03 的资源,其中的信息可能已经有所发展或是发生改变。

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:

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

positivedown:

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.

enmand:

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 :)

positivedown:

I missed that! Thank you!


入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

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