2012-11-15 57 views
2

试图了解如何在Go中解组XML。通读多个示例和stackoverflow问题。我想要的是在系统上安装所有修补程序的一部分。我甚至无法获得解组的补丁,没有错误,只是一个空白的片段。大概做一些基本错误的事情,先谢谢你的任何建议。解决了这个问题xml

<probe version="1.3" date="2012-03-26:17:10"> 
    <properties> 
    </properties> 
    <patches group="server"> 
     <file name="5002012-02-09CR00000server.jar"/> 
     <file name="5002012-02-17CR00001server.jar"/> 
    </patches> 
    <patches group="client"> 
     <file name="5002012-02-09CR00000client.jar"/> 
     <file name="5002012-02-17CR00001client.jar"/> 
    </patches> 
</probe> 
type Patch struct { 
    group string `xml:"group,attr"` 
} 

type Probe struct { 
    XMLName xml.Name `xml"probe"` 
    Patches []Patch `xml:"patches"` 
} 

回答

6

我相信你的问题是在xml包不填充未导出字段。 xml文档说:

由于Unmarshal使用反射包,因此它只能分配给导出(大写)字段。

所有你需要做的是改变groupGroup

type Patch struct { Group string `xml:"group,attr"` } 

您在这里有一个工作示例: http://play.golang.org/p/koSzZr-Bdn

+0

我希望我能投了你更多。那就是问题所在,我尝试了太多变化,并且一路上打错了。 – KaizenSoze

+0

容易发生。我从来没有用过'xml',但是'json'也有类似的问题,它也使用反射。乐意效劳! – ANisus