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

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

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:&quot;soapenv:Envelope&quot;`
		Xmlns          string   `xml:&quot;xmlns:soapenv=http://schemas.xmlsoap.org/soap/envelope/,&quot;`
		CalculatePrice Command  `xml:&quot;soapenv:Body&gt;FunctionName&quot;`
	}

	v := &amp;XMLEnvelop{Xmlns: &quot;namespace1&quot;, CalculatePrice: Command{xml.Name{&quot;namespace2&quot;, &quot;CalculatePrice&quot;}}}

	output, err := xml.MarshalIndent(v, &quot;&quot;, &quot;    &quot;)
	if err != nil {
		fmt.Printf(&quot;error: %v\n&quot;, err)
	}

	// Write the output to check
	os.Stdout.Write(output)

this will give me this output :

&lt;soapenv:Envelope&gt;
    &lt;xmlns:soapenv=http://schemas.xmlsoap.org/soap/envelope/&gt;namespace1&lt;/xmlns:soapenv=http://schemas.xmlsoap.org/soap/envelope/&gt;
    &lt;soapenv:Body&gt;
        &lt;CalculatePrice xmlns=&quot;namespace2&quot;&gt;&lt;/CalculatePrice&gt;
    &lt;/soapenv:Body&gt;
&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 :

        &lt;CalculatePrice xmlns=&quot;namespace2&quot;&gt;
        &lt;test&gt;somevalue&lt;/test&gt;
        &lt;someothertest&gt;somevalue&lt;/someothertest&gt;
        &lt;/CalculatePrice&gt;

答案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",
    },
}

在Playground中测试

英文:

Please Change the struct Command and XMLEnvelop :-

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

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:

确定