亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何解析具有相同名稱的嵌套節點的 XML?

如何解析具有相同名稱的嵌套節點的 XML?

Go
catspeake 2023-06-05 13:22:39
我是 Golang 新手,使用同名嵌套節點解析 XML 對我來說太難了。這是從第三方 API 中提取的 XML:<?xml version="1.0" encoding="UTF-8"?><gesmes:Envelope xmlns:gesmes="http://www.gesmes.org/xml/2002-08-01" xmlns="http://www.ecb.int/vocabulary/2002-08-01/eurofxref">    <gesmes:subject>Reference rates</gesmes:subject>    <gesmes:Sender>        <gesmes:name>European Central Bank</gesmes:name>    </gesmes:Sender>    <Cube>        <Cube time="2019-01-28">            <Cube currency="USD" rate="1.1418"/>            <Cube currency="JPY" rate="124.94"/>            <Cube currency="BGN" rate="1.9558"/>        </Cube>        <Cube time="2019-01-25">            <Cube currency="USD" rate="1.1346"/>            <Cube currency="JPY" rate="124.72"/>            <Cube currency="BGN" rate="1.9558"/>        </Cube>    </Cube></gesmes:Envelope>我需要解析它,所以我有這樣的輸出:&{Rates:[{Currency:USD Rate:1.1418 Date:2019-01-28} {Currency:JPY Rate:124.94 Date:2019-01-28} {Currency:BGN Rate:1.9558 Date:2019-01-28} {Currency:USD Rate:1.1346 Date:2019-01-25} {Currency:JPY Rate:124.72 Date:2019-01-25} {Currency:BGN Rate:1.9558 Date:2019-01-25}]}這是我的代碼:package mainimport (    "encoding/xml"    "fmt")type Rate struct {    Currency  string `xml:"currency,attr"`    Rate      string `xml:"rate,attr"`    Date    string `xml:"time,attr"`}type Rates struct {    Rates []Rate `xml:"Cube>Cube>Cube"`}func main() {    v := &Rates{}    if err := xml.Unmarshal([]byte(src), v); err != nil {        panic(err)    }    fmt.Printf("%+v\n\n", v)}const src = `<?xml version="1.0" encoding="UTF-8"?><gesmes:Envelope xmlns:gesmes="http://www.gesmes.org/xml/2002-08-01" xmlns="http://www.ecb.int/vocabulary/2002-08-01/eurofxref">    <gesmes:subject>Reference rates</gesmes:subject>    <gesmes:Sender>        <gesmes:name>European Central Bank</gesmes:name>    </gesmes:Sender>我不知道如何將time屬性插入 Rates 對象。任何幫助,將不勝感激。這是在 golang游樂場
查看完整描述

2 回答

?
守著星空守著你

TA貢獻1799條經驗 獲得超8個贊

您可以實施自定義xml.Unmarshaler以獲得所需的結果。

type Rate struct {

? ? Currency string `xml:"currency,attr"`

? ? Rate? ? ?string `xml:"rate,attr"`

? ? Date? ? ?string `xml:"time,attr"`

}


type Rates struct {

? ? Rates RateList `xml:"Cube>Cube"`

}


type RateList []Rate


func (ls *RateList) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {

? ? date := start.Attr[0].Value


? ? for {

? ? ? ? tok, err := d.Token()

? ? ? ? if err != nil {

? ? ? ? ? ? if err == io.EOF {

? ? ? ? ? ? ? ? return nil

? ? ? ? ? ? }

? ? ? ? ? ? return err

? ? ? ? }


? ? ? ? if se, ok := tok.(xml.StartElement); ok {

? ? ? ? ? ? rate := Rate{Date: date}

? ? ? ? ? ? if err := d.DecodeElement(&rate, &se); err != nil {

? ? ? ? ? ? ? ? return err

? ? ? ? ? ? }


? ? ? ? ? ? *ls = append(*ls, rate)

? ? ? ? }

? ? }

}

去游樂場


查看完整回答
反對 回復 2023-06-05
?
慕斯709654

TA貢獻1840條經驗 獲得超5個贊

確保您要么信任 XML,要么事先使用XSD之類的東西驗證/清理它。出于安全原因,以及為了能夠回顧并查看您“解組”的數據是否正確,應該這樣做,因為當您處理大量 XML 時,其中一些肯定會被破壞。


查看完整回答
反對 回復 2023-06-05
  • 2 回答
  • 0 關注
  • 168 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號