英文:
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:"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
得分: 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:"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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论