dongling2038 2017-02-22 06:16
浏览 81
已采纳

使用Go无法解析XML

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:"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
}

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

This is the 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>

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

  • 写回答

1条回答 默认 最新

  • duandi2853 2017-02-22 09:18
    关注

    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 (
        "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)
    
    }
    

    Here is the play link : play.golang

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥85 maple软件,solve求反函数,出现rootof怎么办?
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题