0

I'm trying to write a piece of code in golang which takes an xml and Unmarshall it. I'm pretty sure that I've done something wrong at the end of my structure but I can't figure out what. So I would appreciate if someone can help me by telling why the printed rate doesn't have any value, and how I could fix this issue. Thanks a lot!

package main import ( "encoding/xml" "fmt" "log" ) type DataSet struct { XMLName xml.Name `xml:"DataSet"` Header Header `xml:"Header"` Body Body `xml:"Body"` } type Header struct { XMLName xml.Name `xml:"Header"` Publisher string `xml:"Publisher"` PublishingDate string `xml:"PublishingDate"` MessageType string `xml:"MessageType"` } type Body struct { XMLName xml.Name `xml:"Body"` Subject string `xml:"Subject"` OrigCurrency string `xml:"OrigCurrency"` Cube Cube `xml:"Cube"` } type Cube struct { XMLName xml.Name `xml:"Cube"` Date string `xml:"date,attr"` Rate []Rate `xml:"Rate"` } type Rate struct { XMLName xml.Name `xml:"Rate"` Currency string `xml:"currency,attr"` Rate string `xml:"Rate"` } func main() { myxml := `<DataSet xmlns="http://www.bnr.ro/xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.bnr.ro/xsd nbrfxrates.xsd"> <Header> <Publisher>National Bank of Romania</Publisher> <PublishingDate>2020-05-07</PublishingDate> <MessageType>DR</MessageType> </Header> <Body> <Subject>Reference rates</Subject> <OrigCurrency>RON</OrigCurrency> <Cube date="2020-05-07"> <Rate currency="AED">1.2169</Rate> <Rate currency="AUD">2.8874</Rate> <Rate currency="BGN">2.4666</Rate> <Rate currency="BRL">0.7820</Rate> <Rate currency="CAD">3.1747</Rate> <Rate currency="CHF">4.5825</Rate> <Rate currency="CNY">0.6303</Rate> <Rate currency="CZK">0.1779</Rate> <Rate currency="DKK">0.6466</Rate> <Rate currency="EGP">0.2838</Rate> <Rate currency="EUR">4.8242</Rate> <Rate currency="GBP">5.5241</Rate> <Rate currency="HRK">0.6373</Rate> <Rate currency="HUF" multiplier="100">1.3776</Rate> <Rate currency="INR">0.0589</Rate> <Rate currency="JPY" multiplier="100">4.1970</Rate> <Rate currency="KRW" multiplier="100">0.3648</Rate> <Rate currency="MDL">0.2510</Rate> <Rate currency="MXN">0.1852</Rate> <Rate currency="NOK">0.4349</Rate> <Rate currency="NZD">2.7048</Rate> <Rate currency="PLN">1.0618</Rate> <Rate currency="RSD">0.0410</Rate> <Rate currency="RUB">0.0606</Rate> <Rate currency="SEK">0.4545</Rate> <Rate currency="THB">0.1380</Rate> <Rate currency="TRY">0.6161</Rate> <Rate currency="UAH">0.1668</Rate> <Rate currency="USD">4.4695</Rate> <Rate currency="XAU">243.2582</Rate> <Rate currency="XDR">6.0847</Rate> <Rate currency="ZAR">0.2406</Rate> </Cube> </Body> </DataSet>` var currencies DataSet // we unmarshal our byteArray which contains our // myxml content into 'currencies' which we defined above err := xml.Unmarshal([]byte(myxml), &currencies) if err != nil {log.Fatal(err)} for i := 0; i < len(currencies.Body.Cube.Rate); i++ { fmt.Println(currencies.Body.Cube.Rate[i].Currency) fmt.Println(currencies.Body.Cube.Rate[i].Rate) if currencies.Body.Cube.Rate[i].Currency=="EUR"{ fmt.Println("EUR rate is: " + currencies.Body.Cube.Rate[i].Rate) } } } 
1

1 Answer 1

1

Try changing Rate struct to this :

type Rate struct { XMLName xml.Name `xml:"Rate"` Currency string `xml:"currency,attr"` Multiplier string `xml:"multiplier,attr"` Rate string `xml:",chardata"` } 

And I often use this site to convert XML to Go struct. You can try it too: https://www.onlinetool.io/xmltogo/

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.