dongrou839975 2016-04-07 21:22
浏览 84
已采纳

Golang嵌套,重命名的XML属性

Given the following struct:

type book struct {
    XMLName xml.Name   `xml:"DailyAct"`
    Symbol     string  `xml:"TradeInstrId,attr"`
    EntityId   string  `xml:"EntityId,attr"`
    AssetClass string  `xml:"AssetClass,attr"`
    Open       int     `xml:"Open"`
    High       int     `xml:"High"`
    Low        int     `xml:"Low"`
    Close      int     `xml:"Close"`
    // Type      string `` //I'll leave this for another question
}

My current XML:

  <DailyAct EntityId="foo" AssetClass="bar" TradeInstrId="Symbol" >
      <Open>2</Open>
      <High>3</High>
      <Low>1</Low>
      <Close>5</Close>
  </DailyAct>

But, I need to repurpose certain fields of the struct (or generate xml another way) to achieve:

<DailyAct EntityId="foo" AssetClass="bar" TradeInstrId="Symbol">
  <Open Price="2" Type="IND"/>
  <High Price="6" Type="IND"/>
  <Low Price="1" Type="IND"/>
  <Close Price="4" Type="IND"/>
</DailyAct>

But I get: &errors.errorString{s:"xml: DailyAct>Open chain not valid with Price,attr flag"} (actual) when I try to nest fields like so:

type book struct {
    //...
    Open       int     `xml:"DailyAct>Open,Price,attr>"`
    //...
}

Edit: I found this discussion, while googling around so what I'm going for may not be feasible currently

  • 写回答

1条回答 默认 最新

  • dongzhi6927 2016-04-08 00:36
    关注

    You are right currently it's impossible. But you can use sub-structures like

    type PriceType struct {
        Price int    `xml:"Price,attr"`
        Type  string `xml:"Type,attr"`
    }
    
    type Book struct {
        XMLName    xml.Name  `xml:"DailyAct"`
        Symbol     string    `xml:"TradeInstrId,attr"`
        EntityId   string    `xml:"EntityId,attr"`
        AssetClass string    `xml:"AssetClass,attr"`
        Open       PriceType `xml:"Open"`
        High       PriceType `xml:"High"`
        Low        PriceType `xml:"Low"`
        Close      PriceType `xml:"Close"`
    }
    

    Example here http://play.golang.org/p/Ekd6Xf3miS

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 CARSIM前车变道设置
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败