dongzi0602 2016-06-02 07:42
浏览 36

Golang XML解析

This is my XML data:

<disk type='file' device='disk'>
    <driver name='qemu' type='qcow2'/>
    <source file='/var/lib/libvirt/images/generic.qcow2'/>
    <backingStore/>
    <target dev='hda' bus='ide'/>
    <alias name='ide0-0-0'/>
    <address type='drive' controller='0' bus='0' target='0' unit='0'/>
</disk>

and my struct is:

// struct for get device details from xml
type DiskXmlInfo struct {
    Devices []Disk `xml:"devices>disk"`
}

type Disk struct {
    Type string `xml:"device,attr"`
    // Name string `xml:"target>dev,attr"`
    Name string `xml:"target>dev,attr"`
}

I cannot get the target attribute name. How to get the target attribute name?

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • doudu5498 2016-06-02 09:30
    关注

    You can't read attributes with path, like "target> dev,attr". One option is to use separate type for the target, as you already use for the disk:

    type Target struct {
      Dev string `xml:"dev,attr"`
      Bus string `xml:"bus,attr"`
    }
    
    type Disk struct {
      ...
      Target Target `xml:"target"`
    }
    

    Another option is to use custom unmarshaller.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分