0
我收到了一個xml響應,其中包含<Rights></Rights>
塊中的多個條目。它有幾個<Name></Name>
和<Access></Access>
字段。解析xml中的字段
<?xml version="1.0" encoding="UTF-8"?>
<SessionInfo>
<SID>000000000000</SID>
<Challenge>1337</Challenge>
<BlockTime>0</BlockTime>
<Rights>
<Name>Dial</Name>
<Access>2</Access>
<Name>App</Name>
<Access>2</Access>
<Name>HomeAuto</Name>
<Access>2</Access>
<Name>BoxAdmin</Name>
<Access>2</Access>
<Name>Phone</Name>
<Access>2</Access>
<Name>NAS</Name>
<Access>2</Access>
</Rights>
</SessionInfo>
我想將此轉換爲一個權利結構。
type sessionInfo struct {
XMLName xml.Name `xml:"SessionInfo"`
SID string `xml:"SID"`
Challenge string `xml:"Challenge"`
BlockTime uint `xml:"BlockTime"`
Rights []rights `xml:"Rights"`
}
type rights struct {
Name string `xml:"Name"`
Access int `xml:"Access"`
}
不幸的是,它只寫入最後一個元素到數組中。無需編寫自己的解碼器,是否可以在Go中執行此操作?
<SessionInfo>
<SID>000000000000</SID>
<Challenge>1337</Challenge>
<BlockTime>0</BlockTime>
<Rights>
<Name>NAS</Name>
<Access>2</Access>
</Rights>
</SessionInfo>
你可以在這裏測試:https://play.golang.org/p/29I2GPttOz