第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何正確解析xml

如何正確解析xml

Go
嗶嗶one 2021-12-07 18:29:50
我想創(chuàng)建 structs = 每種類型的命令。命令具有 xml 的公共部分 - CommandResult。我創(chuàng)建了接口命令。我需要SomeCommand實(shí)現(xiàn)Command并且可以解析為xml,而且CommandResult中必須實(shí)現(xiàn)IsError,其他功能必須通過SomeCommand來實(shí)現(xiàn)。代碼:type Command interface {    IsError() bool    Request(buf *bufio.Writer, params interface{}) error    ...}// Result of requesttype CommandResult struct {    Code    int    `xml:"code,attr" json:"code"`    Message string `xml:"msg" json:"msg"`}// this Command's func is realized by CommandResult func (self CommandResult) IsError() bool {    return true}// some commandtype SomeCommand struct {    CommandResult // CommandResult `xml:"response>result" json:"result"`}// this Command's func is realized by SomeCommand func (self SomeCommand) Request(buf *bufio.Writer, params interface{}) error {    return nil}// other Command's functions are realized by CommandResult tooXML:<epp>  <response>    <result code="1000">      <msg>Command completed successfully</msg>    </result>    <trID>      <svTRID>asd</svTRID>    </trID>  </response></epp>預(yù)期結(jié)果:a := SomeCommandxml.NewDecoder(reader).Decode(&a)// a.CommandResult.Code = 1000// a.CommandResult.Message = 'Command completed successfully'// a implements Command
查看完整描述

1 回答

?
拉風(fēng)的咖菲貓

TA貢獻(xiàn)1995條經(jīng)驗(yàn) 獲得超2個(gè)贊

我認(rèn)為嵌入結(jié)構(gòu)中的路徑應(yīng)該是絕對的,因?yàn)樗小案浮苯Y(jié)構(gòu)都是“子”的一部分。所以你的


 type CommandResult struct {

     Code    int    `xml:"code,attr" json:"code"`

     Message string `xml:"msg" json:"msg"`

 }

應(yīng)該更像


 type CommandResult struct {

     Code    int    `xml:"response>result>code,attr" json:"code"`

     Message string `xml:"response>result>msg" json:"msg"`

 }

但!我們正面臨著 Go 的局限性。


您不能attr與鏈接一起使用。Github 上有問題,但看起來不在優(yōu)先級列表中。因此,如果我正確理解您的CommandResult聲明的最短版本將是:


type CommandResult struct {

    Result struct {

        Code    int    `xml:"code,attr" json:"code"`

        Message string `xml:"msg" json:"msg"`

    } `xml:"response>result" json:"response"`

}

不是一個(gè)真正的問題,但如果您決定轉(zhuǎn)換Command回 XML 會(huì)很好地聲明它的XMLName. 就像是


type CommandResult struct {

    XMLName xml.Name `xml:"epp"`

    Result  struct {

        Code    int    `xml:"code,attr" json:"code"`

        Message string `xml:"msg" json:"msg"`

    } `xml:"response>result" json:"response"`

}

因?yàn)闆]有它 XML 編碼器會(huì)產(chǎn)生類似 <SomeCommand><response>...</response></SomeCommand>


使用完整示例更新


package main


import (

    "bufio"

    "encoding/xml"

    "log"

)


type Command interface {

    IsError() bool


    Request(buf *bufio.Writer, params interface{}) error

}


// Result of request

type CommandResult struct {

    XMLName xml.Name `xml:"epp"`

    Result  struct {

        Code    int    `xml:"code,attr" json:"code"`

        Message string `xml:"msg" json:"msg"`

    } `xml:"response>result" json:"response"`

}


// this Command's func is realized by CommandResult

func (self CommandResult) IsError() bool {

    return true

}


// some command

type SomeCommand struct {

    CommandResult

}


// this Command's func is realized by SomeCommand

func (self SomeCommand) Request(buf *bufio.Writer, params interface{}) error {

    return nil

}


type AnotherCommand struct {

    CommandResult

}


func (self AnotherCommand) Request(buf *bufio.Writer, params interface{}) error {

    return nil

}


func main() {

    var c Command


    c = SomeCommand{}

    log.Println(c.IsError())


    c = AnotherCommand{}

    log.Println(c.IsError())

}


查看完整回答
反對 回復(fù) 2021-12-07
  • 1 回答
  • 0 關(guān)注
  • 197 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號