英文:
how to make nested xml with goland by go xml marshal
问题
我有一个需要发送 SOAP 请求的 SOAP 请求,需要创建一个 XML 来进行请求。所以我已经创建了以下的 XML:
type Command struct {
XMLName xml.Name
}
type XMLEnvelop struct {
XMLName xml.Name `xml:"soapenv:Envelope"`
Xmlns string `xml:"xmlns:soapenv=http://schemas.xmlsoap.org/soap/envelope/,"`
CalculatePrice Command `xml:"soapenv:Body>FunctionName"`
}
v := &XMLEnvelop{Xmlns: "namespace1", CalculatePrice: Command{xml.Name{"namespace2", "CalculatePrice"}}}
output, err := xml.MarshalIndent(v, "", " ")
if err != nil {
fmt.Printf("error: %v\n", err)
}
// Write the output to check
os.Stdout.Write(output)
这将给我以下输出:
<soapenv:Envelope>
<xmlns:soapenv=http://schemas.xmlsoap.org/soap/envelope/>namespace1</xmlns:soapenv=http://schemas.xmlsoap.org/soap/envelope/>
<soapenv:Body>
<CalculatePrice xmlns="namespace2"></CalculatePrice>
</soapenv:Body>
</soapenv:Envelope>
现在我想在 CalculatePrice
中添加一些字段,但是我在网上找不到相关信息。例如,XML 可以如下所示:
<CalculatePrice xmlns="namespace2">
<test>somevalue</test>
<someothertest>somevalue</someothertest>
</CalculatePrice>
英文:
i have a soap request that i need to make an xml to make the request .
so what i have done to make xml is like below :
type Command struct {
XMLName xml.Name
}
type XMLEnvelop struct {
XMLName xml.Name `xml:"soapenv:Envelope"`
Xmlns string `xml:"xmlns:soapenv=http://schemas.xmlsoap.org/soap/envelope/,"`
CalculatePrice Command `xml:"soapenv:Body>FunctionName"`
}
v := &XMLEnvelop{Xmlns: "namespace1", CalculatePrice: Command{xml.Name{"namespace2", "CalculatePrice"}}}
output, err := xml.MarshalIndent(v, "", " ")
if err != nil {
fmt.Printf("error: %v\n", err)
}
// Write the output to check
os.Stdout.Write(output)
this will give me this output :
<soapenv:Envelope>
<xmlns:soapenv=http://schemas.xmlsoap.org/soap/envelope/>namespace1</xmlns:soapenv=http://schemas.xmlsoap.org/soap/envelope/>
<soapenv:Body>
<CalculatePrice xmlns="namespace2"></CalculatePrice>
</soapenv:Body>
</soapenv:Envelope>
now what i want to make some fields inside the calculateprice
i could not find it on the net so as example the xml would be like :
<CalculatePrice xmlns="namespace2">
<test>somevalue</test>
<someothertest>somevalue</someothertest>
</CalculatePrice>
答案1
得分: 1
请更改结构体Command和XMLEnvelop的代码如下:
type Command struct {
Xmlns string `xml:"xmlns,attr"`
Test string `xml:"test"`
Someothertest string `xml:"someothertest"`
}
type XMLEnvelop struct {
XMLName xml.Name `xml:"soapenv:Envelope"`
Xmlns string `xml:"xmlns:soapenv,attr"`
CalculatePrice Command `xml:"soapenv:Body>CalculatePrice"`
}
并将v := &XMLEnvelop{Xmlns: "namespace1", CalculatePrice: Command{Xmlns: "namespace2",Test: "somevalue", Someothertest: "somevalue"}}
更改为:
v := &XMLEnvelop{
Xmlns: "namespace1",
CalculatePrice: Command{
Xmlns: "namespace2",
Test: "somevalue",
Someothertest: "somevalue",
},
}
英文:
Please Change the struct Command and XMLEnvelop :-
type Command struct {
Xmlns string `xml:"xmlns,attr"`
Test string `xml:"test"`
Someothertest string `xml:"someothertest"`
}
type XMLEnvelop struct {
XMLName xml.Name `xml:"soapenv:Envelope"`
Xmlns string `xml:"xmlns:soapenv=http://schemas.xmlsoap.org/soap/envelope/,"`
CalculatePrice Command `xml:"soapenv:Body>CalculatePrice"`
}
And change the v := &XMLEnvelop{Xmlns: "namespace1", CalculatePrice: Command{Xmlns: "namespace2",Test: "somevalue", Someothertest: "somevalue"}}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论