使用Goland和Go的xml.Marshal函数如何创建嵌套的XML呢?

huangapple go评论117阅读模式
英文:

how to make nested xml with goland by go xml marshal

问题

我有一个需要发送 SOAP 请求的 SOAP 请求,需要创建一个 XML 来进行请求。所以我已经创建了以下的 XML:

  1. type Command struct {
  2. XMLName xml.Name
  3. }
  4. type XMLEnvelop struct {
  5. XMLName xml.Name `xml:"soapenv:Envelope"`
  6. Xmlns string `xml:"xmlns:soapenv=http://schemas.xmlsoap.org/soap/envelope/,"`
  7. CalculatePrice Command `xml:"soapenv:Body>FunctionName"`
  8. }
  9. v := &XMLEnvelop{Xmlns: "namespace1", CalculatePrice: Command{xml.Name{"namespace2", "CalculatePrice"}}}
  10. output, err := xml.MarshalIndent(v, "", " ")
  11. if err != nil {
  12. fmt.Printf("error: %v\n", err)
  13. }
  14. // Write the output to check
  15. os.Stdout.Write(output)

这将给我以下输出:

  1. <soapenv:Envelope>
  2. <xmlns:soapenv=http://schemas.xmlsoap.org/soap/envelope/>namespace1</xmlns:soapenv=http://schemas.xmlsoap.org/soap/envelope/>
  3. <soapenv:Body>
  4. <CalculatePrice xmlns="namespace2"></CalculatePrice>
  5. </soapenv:Body>
  6. </soapenv:Envelope>

现在我想在 CalculatePrice 中添加一些字段,但是我在网上找不到相关信息。例如,XML 可以如下所示:

  1. <CalculatePrice xmlns="namespace2">
  2. <test>somevalue</test>
  3. <someothertest>somevalue</someothertest>
  4. </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 :

  1. type Command struct {
  2. XMLName xml.Name
  3. }
  4. type XMLEnvelop struct {
  5. XMLName xml.Name `xml:&quot;soapenv:Envelope&quot;`
  6. Xmlns string `xml:&quot;xmlns:soapenv=http://schemas.xmlsoap.org/soap/envelope/,&quot;`
  7. CalculatePrice Command `xml:&quot;soapenv:Body&gt;FunctionName&quot;`
  8. }
  9. v := &amp;XMLEnvelop{Xmlns: &quot;namespace1&quot;, CalculatePrice: Command{xml.Name{&quot;namespace2&quot;, &quot;CalculatePrice&quot;}}}
  10. output, err := xml.MarshalIndent(v, &quot;&quot;, &quot; &quot;)
  11. if err != nil {
  12. fmt.Printf(&quot;error: %v\n&quot;, err)
  13. }
  14. // Write the output to check
  15. os.Stdout.Write(output)

this will give me this output :

  1. &lt;soapenv:Envelope&gt;
  2. &lt;xmlns:soapenv=http://schemas.xmlsoap.org/soap/envelope/&gt;namespace1&lt;/xmlns:soapenv=http://schemas.xmlsoap.org/soap/envelope/&gt;
  3. &lt;soapenv:Body&gt;
  4. &lt;CalculatePrice xmlns=&quot;namespace2&quot;&gt;&lt;/CalculatePrice&gt;
  5. &lt;/soapenv:Body&gt;
  6. &lt;/soapenv:Envelope&gt;

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 :

  1. &lt;CalculatePrice xmlns=&quot;namespace2&quot;&gt;
  2. &lt;test&gt;somevalue&lt;/test&gt;
  3. &lt;someothertest&gt;somevalue&lt;/someothertest&gt;
  4. &lt;/CalculatePrice&gt;

答案1

得分: 1

请更改结构体Command和XMLEnvelop的代码如下:

  1. type Command struct {
  2. Xmlns string `xml:"xmlns,attr"`
  3. Test string `xml:"test"`
  4. Someothertest string `xml:"someothertest"`
  5. }
  6. type XMLEnvelop struct {
  7. XMLName xml.Name `xml:"soapenv:Envelope"`
  8. Xmlns string `xml:"xmlns:soapenv,attr"`
  9. CalculatePrice Command `xml:"soapenv:Body>CalculatePrice"`
  10. }

并将v := &XMLEnvelop{Xmlns: "namespace1", CalculatePrice: Command{Xmlns: "namespace2",Test: "somevalue", Someothertest: "somevalue"}}更改为:

  1. v := &XMLEnvelop{
  2. Xmlns: "namespace1",
  3. CalculatePrice: Command{
  4. Xmlns: "namespace2",
  5. Test: "somevalue",
  6. Someothertest: "somevalue",
  7. },
  8. }

在Playground中测试

英文:

Please Change the struct Command and XMLEnvelop :-

  1. type Command struct {
  2. Xmlns string `xml:&quot;xmlns,attr&quot;`
  3. Test string `xml:&quot;test&quot;`
  4. Someothertest string `xml:&quot;someothertest&quot;`
  5. }
  1. type XMLEnvelop struct {
  2. XMLName xml.Name `xml:&quot;soapenv:Envelope&quot;`
  3. Xmlns string `xml:&quot;xmlns:soapenv=http://schemas.xmlsoap.org/soap/envelope/,&quot;`
  4. CalculatePrice Command `xml:&quot;soapenv:Body&gt;CalculatePrice&quot;`
  5. }

And change the v := &amp;XMLEnvelop{Xmlns: &quot;namespace1&quot;, CalculatePrice: Command{Xmlns: &quot;namespace2&quot;,Test: &quot;somevalue&quot;, Someothertest: &quot;somevalue&quot;}}

Test In Playground

huangapple
  • 本文由 发表于 2021年9月22日 01:06:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/69272865.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定