在Go语言中解析XML时遇到了问题。

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

Trouble Parsing XML using Go

问题

我一直在尝试使用Go语言中定义结构体并使用xml.Unmarshal解析XML文件,代码如下:

type InitiateResponse struct {
    SoapenvEnvelope struct {
        SoapenvBody struct {
            ReqResponseMsg struct {
                CData struct {
                    Response struct {
                        ResponseCode              string `xml:"ResponseCode"`
                        ConversationID            string `xml:"ConversationID"`
                        ResponseDesc              string `xml:"ResponseDesc"`
                        OriginatorConversationID string `xml:"OriginatorConversationID"`
                        ServiceStatus             string `xml:"ServiceStatus"`
                    } `xml:"req:Response"`
                } `xml:"![CDATA["`
            } `xml:"req:ResponseMsg"`
        } `xml:"soapenv:Body"`
    } `xml:"soapenv:Envelope"`
}

var unit InitiateResponse
if err := xml.Unmarshal([]byte(data), &unit); err != nil {
    fmt.Println(err)
    return
}

在这段代码中没有抛出错误,但是结构体在最后始终为空。不明白为什么。

这是XML文件的内容:

<?xml version='1.0' encoding='UTF-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:req="http://cps.huawei.com/cpsinterface/request">
   <soapenv:Header />
   <soapenv:Body>
      <req:ResponseMsg><![CDATA[<?xml version="1.0" encoding="UTF-8"?><Response><ResponseCode>14</ResponseCode><ConversationID>AG_20170222_000040cdc5cecf730e39</ConversationID><ResponseDesc>The caller information is invalid.</ResponseDesc><OriginatorConversationID>S_X2013012921001</OriginatorConversationID><ServiceStatus>2</ServiceStatus></Response>]]></req:ResponseMsg>
   </soapenv:Body>
</soapenv:Envelope>

请查看以下链接:
https://play.golang.org/p/Irmy8AsUKa

英文:

I've been trying to parse an xml in Go by defining structs and using xml.Unmarshal like this:

type InitiateResponse struct {
	SoapenvEnvelope struct {
		SoapenvBody struct {
			ReqResponseMsg struct {
				CData struct {
					Response struct {
						ResponseCode string `xml:&quot;ResponseCode&quot;`
						ConversationID string `xml:&quot;ConversationID&quot;`
						ResponseDesc string `xml:&quot;ResponseDesc&quot;`
						OriginatorConversationID string `xml:&quot;OriginatorConversationID&quot;`
						ServiceStatus string `xml:&quot;ServiceStatus&quot;`
					} `xml:&quot;req:Response&quot;`
				} `xml: ![CDATA[`
			} `xml:&quot;req:ResponseMsg&quot;`
		} `xml:&quot;soapenv:Body&quot;`
	} `xml:&quot;soapenv:Envelope&quot;`
} 	

var unit InitiateResponse
if err := xml.Unmarshal([]byte(data), &amp;unit); err != nil {
	fmt.Println(err)
	return
}

There's no error thrown but the struct is always empty at the end. Can't understand why.

This is the xml:

&lt;?xml version=&#39;1.0&#39; encoding=&#39;UTF-8&#39;?&gt;&lt;soapenv:Envelope xmlns:soapenv=&quot;http://schemas.xmlsoap.org/soap/envelope/&quot; xmlns:req=&quot;http://cps.huawei.com/cpsinterface/request&quot;&gt;
   &lt;soapenv:Header /&gt;
   &lt;soapenv:Body&gt;
      &lt;req:ResponseMsg&gt;&lt;![CDATA[&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;&lt;Response&gt;&lt;ResponseCode&gt;14&lt;/ResponseCode&gt;&lt;ConversationID&gt;AG_20170222_000040cdc5cecf730e39&lt;/ConversationID&gt;&lt;ResponseDesc&gt;The caller information is invalid.&lt;/ResponseDesc&gt;&lt;OriginatorConversationID&gt;S_X2013012921001&lt;/OriginatorConversationID&gt;&lt;ServiceStatus&gt;2&lt;/ServiceStatus&gt;&lt;/Response&gt;]]&gt;&lt;/req:ResponseMsg&gt;
   &lt;/soapenv:Body&gt;
&lt;/soapenv:Envelope&gt;

Check it out:
https://play.golang.org/p/Irmy8AsUKa

答案1

得分: 4

CDATA无法解组为结构体。

  • 如果XML元素包含字符数据,则该数据将累积在具有标签",chardata"的第一个结构字段中。结构字段可以是[]byte或string类型。如果没有这样的字段,则字符数据将被丢弃。

尝试将CDATA解组为结构体将失败,并显示以下错误:

无法解组为结构体

这个链接的示例解释了上述问题。

结构体存在其他问题。Go目前不支持XML命名空间前缀。这是一个关于相同问题的开放问题https://github.com/golang/go/issues/9519

以下是重新编写的代码:

代码

package main

import (
	"encoding/xml"
	"fmt"
)

const data = `<soapenv:Envelope>
    <soapenv:Body>
        <req:ResponseMsg>
        <name>blah</name>
            <![CDATA[<?xml version="1.0" encoding="UTF-8"?><Response><ResponseCode>14</ResponseCode><ConversationID>AG_20170222_00006f8794f700eff099</ConversationID><ResponseDesc>The caller information is invalid.</ResponseDesc><OriginatorConversationID>S_X2013012921001</OriginatorConversationID><ServiceStatus>2</ServiceStatus></Response>]]>
        </req:ResponseMsg>
    </soapenv:Body>
</soapenv:Envelope>`

type Envelop struct {
	XMLName     xml.Name `xml:"Envelope"`
	ResponseMsg struct {
		CDATA []byte `xml:",cdata"`
	} `xml:"Body>ResponseMsg"`
}

type Response struct {
	XMLName                  xml.Name `xml:"Response"`
	ResponseCode             string   `xml:"ResponseCode"`
	ConversationID           string   `xml:"ConversationID"`
	ResponseDesc             string   `xml:"ResponseDesc"`
	OriginatorConversationID string   `xml:"OriginatorConversationID"`
	ServiceStatus            string   `xml:"ServiceStatus"`
}

func main() {
	var unit Envelop
	if err := xml.Unmarshal([]byte(data), &unit); err != nil {
		fmt.Println(err)
		return
	}
	responseBytes := unit.ResponseMsg.CDATA
	var response Response
	if err := xml.Unmarshal([]byte(responseBytes), &response); err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println("Response: ", response)
}

这是playground链接:play.golang

英文:

CDATA can not be unmarshalled to a struct.

> * If the XML element contains character data, that data is accumulated in the first struct field that has tag ",chardata". The
> struct field may have type []byte or string. If there is no such
> field, the character data is discarded.

An attempt to unmarshal CDATA to struct will fail with following error

cannot unmarshal into struct 

This playground link explains the above mentioned problem.

There were some other issues with the struct.Go currently does not support XML namespace prefixes .Here is an open issue regarding the same https://github.com/golang/go/issues/9519.

Here is code re-written

Code

package main

import (
	&quot;encoding/xml&quot;
	&quot;fmt&quot;
)

const data = `&lt;soapenv:Envelope&gt;
    &lt;soapenv:Body&gt;
        &lt;req:ResponseMsg&gt;
        &lt;name&gt;blah&lt;/name&gt;
            &lt;![CDATA[&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;&lt;Response&gt;&lt;ResponseCode&gt;14&lt;/ResponseCode&gt;&lt;ConversationID&gt;AG_20170222_00006f8794f700eff099&lt;/ConversationID&gt;&lt;ResponseDesc&gt;The caller information is invalid.&lt;/ResponseDesc&gt;&lt;OriginatorConversationID&gt;S_X2013012921001&lt;/OriginatorConversationID&gt;&lt;ServiceStatus&gt;2&lt;/ServiceStatus&gt;&lt;/Response&gt;]]&gt;
        &lt;/req:ResponseMsg&gt;
    &lt;/soapenv:Body&gt;
&lt;/soapenv:Envelope&gt;`

type Envelop struct {
	XMLName     xml.Name `xml:&quot;Envelope&quot;`
	ResponseMsg struct {
		CDATA []byte `xml:&quot;,cdata&quot;`
	} `xml:&quot;Body&gt;ResponseMsg&quot;`
}

type Response struct {
	XMLName                  xml.Name `xml:&quot;Response&quot;`
	ResponseCode             string   `xml:&quot;ResponseCode&quot;`
	ConversationID           string   `xml:&quot;ConversationID&quot;`
	ResponseDesc             string   `xml:&quot;ResponseDesc&quot;`
	OriginatorConversationID string   `xml:&quot;OriginatorConversationID&quot;`
	ServiceStatus            string   `xml:&quot;ServiceStatus&quot;`
}

func main() {
	var unit Envelop
	if err := xml.Unmarshal([]byte(data), &amp;unit); err != nil {
		fmt.Println(err)
		return
	}
	responseBytes := unit.ResponseMsg.CDATA
	var response Response
	if err := xml.Unmarshal([]byte(responseBytes), &amp;response); err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(&quot;Response : &quot;, response)

}

Here is the play link : play.golang

huangapple
  • 本文由 发表于 2017年2月22日 14:16:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/42384012.html
匿名

发表评论

匿名网友

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

确定