在Golang中使用命名空间消费SOAP

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

Consuming SOAP with namespaces in GOLANG

问题

我是一个GO的新手,开始学习如何处理SOAP请求。我在这里遇到了一个困难,就是不知道如何构建结构体来反映从webservice中传入的这种数据,以便进行解组。你能给我一些建议吗?我正在使用GO 1.5.1版本。

以下是要翻译的内容:

我是一个GO的新手,开始学习如何处理SOAP请求。我在这里遇到了一个困难,就是不知道如何构建结构体来反映从webservice中传入的这种数据,以便进行解组。你能给我一些建议吗?我正在使用GO 1.5.1版本。

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="https://webapi.allegro.pl/service.php">
<SOAP-ENV:Body>
    <ns1:doQueryAllSysStatusResponse>
        <ns1:sysCountryStatus>

            <ns1:item>
                <ns1:countryId>1</ns1:countryId>
                <ns1:programVersion>1.0</ns1:programVersion>
                <ns1:catsVersion>1.1.87</ns1:catsVersion>
                <ns1:apiVersion>1.0</ns1:apiVersion>
                <ns1:attribVersion>1.0</ns1:attribVersion>
                <ns1:formSellVersion>1.4.46</ns1:formSellVersion>
                <ns1:siteVersion>1.0</ns1:siteVersion>
                <ns1:verKey>123131231</ns1:verKey>
            </ns1:item>

            <ns1:item>
                <ns1:countryId>56</ns1:countryId>
                <ns1:programVersion>1.0</ns1:programVersion>   
                <ns1:catsVersion>1.0.43</ns1:catsVersion>
                <ns1:apiVersion>1.0</ns1:apiVersion> 
                <ns1:attribVersion>1.0</ns1:attribVersion>
                <ns1:formSellVersion>1.0.69</ns1:formSellVersion>
                <ns1:siteVersion>1.0</ns1:siteVersion>
                <ns1:verKey>00000101</ns1:verKey>
            </ns1:item>
        </ns1:sysCountryStatus>
    </ns1:doQueryAllSysStatusResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
英文:

i'm a GO newbie, starting to learn how to process SOAP requests. I have a difficulty here with the namespaces: i don't know how to contruct the structs to reflect such kind of data coming in from the webservice in order to unmarshall it. Could you give me a few hints here? I'm using GO 1.5.1

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;SOAP-ENV:Envelope xmlns:SOAP-ENV=&quot;http://schemas.xmlsoap.org/soap/envelope/&quot; xmlns:ns1=&quot;https://webapi.allegro.pl/service.php&quot;&gt;
&lt;SOAP-ENV:Body&gt;
    &lt;ns1:doQueryAllSysStatusResponse&gt;
        &lt;ns1:sysCountryStatus&gt;

            &lt;ns1:item&gt;
                &lt;ns1:countryId&gt;1&lt;/ns1:countryId&gt;
                &lt;ns1:programVersion&gt;1.0&lt;/ns1:programVersion&gt;
                &lt;ns1:catsVersion&gt;1.1.87&lt;/ns1:catsVersion&gt;
                &lt;ns1:apiVersion&gt;1.0&lt;/ns1:apiVersion&gt;
                &lt;ns1:attribVersion&gt;1.0&lt;/ns1:attribVersion&gt;
                &lt;ns1:formSellVersion&gt;1.4.46&lt;/ns1:formSellVersion&gt;
                &lt;ns1:siteVersion&gt;1.0&lt;/ns1:siteVersion&gt;
                &lt;ns1:verKey&gt;123131231&lt;/ns1:verKey&gt;
            &lt;/ns1:item&gt;

            &lt;ns1:item&gt;
                &lt;ns1:countryId&gt;56&lt;/ns1:countryId&gt;
                &lt;ns1:programVersion&gt;1.0&lt;/ns1:programVersion&gt;   
                &lt;ns1:catsVersion&gt;1.0.43&lt;/ns1:catsVersion&gt;
                &lt;ns1:apiVersion&gt;1.0&lt;/ns1:apiVersion&gt; 
                &lt;ns1:attribVersion&gt;1.0&lt;/ns1:attribVersion&gt;
                &lt;ns1:formSellVersion&gt;1.0.69&lt;/ns1:formSellVersion&gt;
                &lt;ns1:siteVersion&gt;1.0&lt;/ns1:siteVersion&gt;
                &lt;ns1:verKey&gt;00000101&lt;/ns1:verKey&gt;
            &lt;/ns1:item&gt;
        &lt;/ns1:sysCountryStatus&gt;
    &lt;/ns1:doQueryAllSysStatusResponse&gt;
&lt;/SOAP-ENV:Body&gt;
&lt;/SOAP-ENV:Envelope&gt;

答案1

得分: 1

你可以使用与你的SOAP数据匹配的结构体,并使用"encoding/xml"包进行解组。

结构体:

type Envelope struct {
	XMLName  xml.Name `xml:"SOAP-ENV:Envelope"`
	Body     Body     `xml:"SOAP-ENV:Body"`
}

type Body struct {
	StatusRes *DoQueryAllSysStatusResponse `xml:"ns1:doQueryAllSysStatusResponse"`
}

type DoQueryAllSysStatusResponse struct {
	CountryStatus *SysCountryStatus `xml:"ns1:sysCountryStatus"`
}

// ...

解组:

data := []byte{} // SOAP数据
env := &Envelope{}
err := xml.Unmarshal(data, env)
if err != nil {
	// 处理错误
}

以上是给定代码的翻译结果。

英文:

You can a structure that matches your SOAP data and then unmarshal it with the "encoding/xml" package

Structs :

type Envelope struct {
	XMLName  xml.Name `xml:&quot;SOAP-ENV:Envelope&quot;`
	Body     Body     `xml:&quot;SOAP-ENV:Body&quot;`
}

type Body struct {
	StatusRes *DoQueryAllSysStatusResponse `xml:&quot;ns1:doQueryAllSysStatusResponse&quot;`
}

type DoQueryAllSysStatusResponse struct {
	CountryStatus *SysCountryStatus `xml:&quot;ns1:sysCountryStatus&quot;`
}

// ...

Unmarshal:

data := []byte{} // SOAP data
env := &amp;Envelope{}
err := xml.Unmarshal(data, env)
if err != nil {
	// do something
}

huangapple
  • 本文由 发表于 2015年10月5日 18:57:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/32946927.html
匿名

发表评论

匿名网友

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

确定