How can I convert xml to struct

I’ve the below xml data, that is related to a product line details:

type catalog struct {
	Id string `xml:"id"`
}

type price struct {
	Subtotal    int    `xml:"subtotal"`
	Currency    string `xml:"currency"`
	Total       int    `xml:"total"`
	PriceStatus string `xml:"price_status"`
}

type image struct {
	Url string `xml:"url"`
	Id  int    `xml:"id"`
}

type product struct {
	Id         int   `xml:"id"`
	RetailerId int   `xml:"retailer_id"`
	Image      image `xml:"image"`
	Price    int    `xml:"price"`
	Currency string `xml:"currency"`
	Name     string `xml:"name"`
	Quantity int    `xml:"quantity"`
}

type order struct {
	Product product `xml:"product"`
	Catalog catalog `xml:"catalog"`
	Price   price   `xml:"price"`
}

type Iq struct {
	XMLName xml.Name `xml:"iq"`
	Order   order    `xml:"order"`
}
<iq from="s.whatsapp.net" id="79.242-3" type="result">
  <order creation_ts="1651699388" id="368282458424984">
    <product>
      <id>8312993582051445</id>
      <retailer_id>291</retailer_id>
      <image>
        <url>https://mmg.whatsapp.net/v/t45.5328-4/279646282_7510595942346471_4295336878174066544_n.jpg?stp=dst-jpg_p100x100&ccb=1-5&_nc_sid=c48759&_nc_ohc=OMyHhkGxzRoAX-9cHVL&_nc_ad=z-m&_nc_cid=0&_nc_ht=mmg.whatsapp.net&oh=01_AVxAZgz_LwkteWpkZhK26OLEZFrg3GTx-cMJP_h8V_vmJQ&oe=62774C93</url>
        <id>7510595939013138</id>
      </image>
      <price>5000</price>
      <currency>SAR</currency>
      <name>coffee</name>
      <quantity>1</quantity>
    </product>
    <catalog><id>326185462964376</id></catalog>
    <price>
      <subtotal>5000</subtotal>
      <currency>SAR</currency>
      <total>5000</total>
      <price_status>provided</price_status>
    </price>
  </order>
</iq>

I tried:

iq := &Iq{}
err := xml.Unmarshal([]byte(contents), &iq)
				if err != nil {
					panic(err)
				}

But did not work!

The playground is: Go Playground - The Go Programming Language

What was the error message?

You probably want var iq Iq instead. Your iq is a pointer to an Iq and then you pass a pointer to that pointer to Unmarshal.

Did not work, I added the playground: Go Playground - The Go Programming Language

Your url tag is invalid XML because it has literal &'s. Replace them with &amp; Go Playground - The Go Programming Language

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.