Golang嵌套、重命名XML属性

huangapple go评论72阅读模式
英文:

Golang nested, renamed XML attributes

问题

给定以下结构体:

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 `` //我将这个留给另一个问题
}

我的当前XML:

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

但是,我需要重新使用结构体的某些字段(或以另一种方式生成XML)来实现:

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

但是当我尝试像这样嵌套字段时,我得到了错误信息:&errors.errorString{s:"xml: DailyAct>Open chain not valid with Price,attr flag"} (actual)

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

编辑:
我在谷歌搜索时找到了这个讨论,所以我目前的做法可能不可行。

英文:

Given the following struct:

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

My current XML:

  &lt;DailyAct EntityId=&quot;foo&quot; AssetClass=&quot;bar&quot; TradeInstrId=&quot;Symbol&quot; &gt;
      &lt;Open&gt;2&lt;/Open&gt;
      &lt;High&gt;3&lt;/High&gt;
      &lt;Low&gt;1&lt;/Low&gt;
      &lt;Close&gt;5&lt;/Close&gt;
  &lt;/DailyAct&gt;

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

&lt;DailyAct EntityId=&quot;foo&quot; AssetClass=&quot;bar&quot; TradeInstrId=&quot;Symbol&quot;&gt;
  &lt;Open Price=&quot;2&quot; Type=&quot;IND&quot;/&gt;
  &lt;High Price=&quot;6&quot; Type=&quot;IND&quot;/&gt;
  &lt;Low Price=&quot;1&quot; Type=&quot;IND&quot;/&gt;
  &lt;Close Price=&quot;4&quot; Type=&quot;IND&quot;/&gt;
&lt;/DailyAct&gt;

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

type book struct {
    //...
	Open	   int 	   `xml:&quot;DailyAct&gt;Open,Price,attr&gt;&quot;`
    //...
}

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

答案1

得分: 6

你是对的,目前是不可能的。但是你可以使用子结构体,例如:

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"`
}

示例代码可以在这里查看:http://play.golang.org/p/Ekd6Xf3miS

英文:

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

type PriceType struct {
	Price int    `xml:&quot;Price,attr&quot;`
	Type  string `xml:&quot;Type,attr&quot;`
}

type Book struct {
	XMLName    xml.Name  `xml:&quot;DailyAct&quot;`
	Symbol     string    `xml:&quot;TradeInstrId,attr&quot;`
	EntityId   string    `xml:&quot;EntityId,attr&quot;`
	AssetClass string    `xml:&quot;AssetClass,attr&quot;`
	Open       PriceType `xml:&quot;Open&quot;`
	High       PriceType `xml:&quot;High&quot;`
	Low        PriceType `xml:&quot;Low&quot;`
	Close      PriceType `xml:&quot;Close&quot;`
}

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

huangapple
  • 本文由 发表于 2016年4月8日 05:22:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/36487467.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定