alebeta
(Alejandro Betancor)
January 8, 2020, 3:24pm
1
Hello all,
I’m making a simple program which perform some XML API calls. In one of the calls I get data in a slice, which contain ID’s. My next step would be to put those ID’s inside of XML body and make another API call.
Normally I would send an XML body like this:
<packet>
<customer>
<get-domain-list>
<filter>
<id>138</id>
</filter>
</get-domain-list>
</customer>
</packet>
but in my case I would like to insert several tags with all the ID’s I have stored in my slice. For example
<packet>
<customer>
<get-domain-list>
<filter>
<id>138</id>
<id>139</id>
<id>140</id>
<id>141</id>
</filter>
</get-domain-list>
</customer>
</packet>
Then I would like to know how I could create the XML body inserting the content of my slice?
Thanks very much,
Alejandro B.
Please check https://www.onlinetool.io/xmltogo/
Just paste your xml and the application generates its correspoding golang structure
type Packet struct {
XMLName xml.Name `xml:"packet"`
Text string `xml:",chardata"`
Customer struct {
Text string `xml:",chardata"`
GetDomainList struct {
Text string `xml:",chardata"`
Filter struct {
Text string `xml:",chardata"`
ID []string `xml:"id"`
} `xml:"filter"`
} `xml:"get-domain-list"`
} `xml:"customer"`
}
alebeta
(Alejandro Betancor)
January 8, 2020, 3:46pm
3
Hi Yamil,
thanks for your help!
I already hace the structure. but my question is, how can I insert in
ID []string `xml:"id"`
my slice which was the result of another api call? I see in the example you pasted here, the ID parameter is a Slice string []string
.
Thanks and all the best
To access the ID slide you can use some code as
package main
import (
"fmt"
"encoding/xml"
)
type Packet struct {
XMLName xml.Name `xml:"packet"`
Text string `xml:",chardata"`
Customer struct {
Text string `xml:",chardata"`
GetDomainList struct {
Text string `xml:",chardata"`
Filter struct {
Text string `xml:",chardata"`
ID []string `xml:"id"`
} `xml:"filter"`
} `xml:"get-domain-list"`
} `xml:"customer"`
}
func main() {
rawXmlData := "<packet><customer><get-domain-list><filter> <id>138</id><id>139</id><id>140</id><id>141</id></filter></get-domain-list></customer></packet>";
var packet Packet
xml.Unmarshal([]byte(rawXmlData), &packet)
fmt.Println(packet.Customer.GetDomainList .Filter.ID)
}
alebeta
(Alejandro Betancor)
January 8, 2020, 4:01pm
5
Hi Yamil,
I understand it, but in this case you are extracting the ID from the XML, in my case I want to insert the ID’s in the XML. I already manage to run unmarshal properly to get some information.
But for what I want to achive, I need to build the XML body and adding the ID’s I already have in my slice.
Maybe I’m not explaining properly what I want to achieve.
thanks
Just create the corresponding field as slide
packet.Customer.GetDomainList .Filter.ID = make([]string, 2)
packet.Customer.GetDomainList .Filter.ID = append(packet.Customer.GetDomainList .Filter.ID , "10001")
packet.Customer.GetDomainList .Filter.ID = append(packet.Customer.GetDomainList .Filter.ID , "10002")
alebeta
(Alejandro Betancor)
January 8, 2020, 4:18pm
7
Thanks! I will give it a try and come back to you!
All the best
system
(system)
Closed
April 7, 2020, 4:21pm
9
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.