Xml 解组到golang中实例化的空接口

Xml 解组到golang中实例化的空接口,xml,go,unmarshalling,Xml,Go,Unmarshalling,我试图将其解组为空go接口类型的变量。具体类型具有适当的xml标记,但由于某些原因,我无法解组xml值。他们只是空着 此代码执行我想要执行的操作: type Address struct { City, State string } type Result struct { XMLName xml.Name `xml:"Person"` Name string `xml:"FullName" json:"FullName"` Address inter

我试图将其解组为空go接口类型的变量。具体类型具有适当的xml标记,但由于某些原因,我无法解组xml值。他们只是空着

此代码执行我想要执行的操作:

type Address struct {
    City, State string
}

type Result struct {
    XMLName xml.Name `xml:"Person"`
    Name    string   `xml:"FullName" json:"FullName"`
    Address interface{}
}

func main() {

    xmlAddress := &Address{}
    xmlResult := &Result{Name: "none", Address: xmlAddress}

    xmlData := `
    <Person>
        <FullName>Mike McCartney</FullName>
        <Address>
            <City>Austin</City>
            <State>Texas</State>
        </Address>
    </Person>
`

    err := xml.Unmarshal([]byte(xmlData), xmlResult)

    // xmlResult = {"XMLName":{"Space":"","Local":"Person"},"FullName":"Mike McCartney","Address":{"City":"Austin","State":"Texas"}}
}
类型地址结构{
城市、州字符串
}
类型结果结构{
XMLName xml.Name`xml:“个人”`
名称字符串`xml:“FullName”json:“FullName”`
地址接口{}
}
func main(){
xmlAddress:=&地址{}
xmlResult:=&Result{Name:“无”,地址:xmlAddress}
xmlData:=`
总裁迈克-麦坎尼

但在我自己的示例中,实际的xml和名称空间不起作用:

type Envelope struct {
    XMLName xml.Name `xml:"http://www.w3.org/2003/05/soap-envelope Envelope"`
    Body    Body     `xml:"http://www.w3.org/2003/05/soap-envelope Body"`
}

type Body struct {
    XMLName xml.Name `xml:"http://www.w3.org/2003/05/soap-envelope Body"`
    Content interface{}
}

type DebtorGetNameResponse struct {
    XMLName             xml.Name `xml:"http://e-conomic.com Debtor_GetNameResponse"`
    DebtorGetNameResult string   `xml:"Debtor_GetNameResult"`
}

func main() {

    xmlData := `
    <?xml version="1.0" encoding="utf-8"?>
    <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
        <soap:Body>
            <Debtor_GetNameResponse xmlns="http://e-conomic.com">
                <Debtor_GetNameResult>THIS SHOULD BE IN THE OUTPUT</Debtor_GetNameResult>
            </Debtor_GetNameResponse>
        </soap:Body>
    </soap:Envelope>`

    e := new(Envelope)
    res := new(DebtorGetNameResponse)
    e.Body = Body{Content: res}

    err := xml.Unmarshal([]byte(xmlData), e)

    // res = {"XMLName":{"Space":"","Local":""},"DebtorGetNameResult":""}
}
类型信封结构{
XMLName xml.Name`xml:“http://www.w3.org/2003/05/soap-envelope 信封“`
Body`xml:“http://www.w3.org/2003/05/soap-envelope “身体”`
}
类型体结构{
XMLName xml.Name`xml:“http://www.w3.org/2003/05/soap-envelope “身体”`
内容接口{}
}
类型debortOrgetNameResponse结构{
XMLName xml.Name`xml:“http://e-conomic.com 债务人\u GetNameResponse“`
DebtorGetNameResult字符串`xml:“债务人\u GetNameResult”`
}
func main(){
xmlData:=`
这应该在输出中
`
e:=新的(信封)
res:=新的(DebtorGetNameResponse)
e、 Body=Body{Content:res}
err:=xml.Unmarshal([]字节(xmlData),e)
//res={“XMLName”:{“Space”:“,“Local”:”},“debortOrgetNameResult”:”}
}

完整代码:

您需要将xml标记添加到
接口{}
,即

Content interface{} `xml:"http://e-conomic.com Debtor_GetNameResponse"`

地址接口{}
在您的另一个示例中之所以有效,是因为它的名称与xml标记
相同,并且
通过它进行查找。

您不能实例化接口,您只能拥有一个接口类型的变量,该变量包含一个具体类型的实例。@Adrian,这就是我的意思。我已经编辑了这个问题。请在这里提供相关代码示例。你是对的。伙计,我真的不喜欢go中的xml处理。它仍然比其他语言好很多。。。