如何正确解析xml

我想创建 structs = 每种类型的命令。


命令具有 xml 的公共部分 - CommandResult。我创建了接口命令。我需要SomeCommand实现Command并且可以解析为xml,而且CommandResult中必须实现IsError,其他功能必须通过SomeCommand来实现。


代码:


type Command interface {

    IsError() bool


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

    ...

}


// Result of request

type 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 command

type 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 too

XML:


<epp>

  <response>

    <result code="1000">

      <msg>Command completed successfully</msg>

    </result>

    <trID>

      <svTRID>asd</svTRID>

    </trID>

  </response>

</epp>

预期结果:


a := SomeCommand

xml.NewDecoder(reader).Decode(&a)

// a.CommandResult.Code = 1000

// a.CommandResult.Message = 'Command completed successfully'


// a implements Command


哔哔one
浏览 161回答 1
1回答

拉风的咖菲猫

我认为嵌入结构中的路径应该是绝对的,因为所有“父”结构都是“子”的一部分。所以你的&nbsp;type CommandResult struct {&nbsp; &nbsp; &nbsp;Code&nbsp; &nbsp; int&nbsp; &nbsp; `xml:"code,attr" json:"code"`&nbsp; &nbsp; &nbsp;Message string `xml:"msg" json:"msg"`&nbsp;}应该更像&nbsp;type CommandResult struct {&nbsp; &nbsp; &nbsp;Code&nbsp; &nbsp; int&nbsp; &nbsp; `xml:"response>result>code,attr" json:"code"`&nbsp; &nbsp; &nbsp;Message string `xml:"response>result>msg" json:"msg"`&nbsp;}但!我们正面临着 Go 的局限性。您不能attr与链接一起使用。Github 上有问题,但看起来不在优先级列表中。因此,如果我正确理解您的CommandResult声明的最短版本将是:type CommandResult struct {&nbsp; &nbsp; Result struct {&nbsp; &nbsp; &nbsp; &nbsp; Code&nbsp; &nbsp; int&nbsp; &nbsp; `xml:"code,attr" json:"code"`&nbsp; &nbsp; &nbsp; &nbsp; Message string `xml:"msg" json:"msg"`&nbsp; &nbsp; } `xml:"response>result" json:"response"`}不是一个真正的问题,但如果您决定转换Command回 XML 会很好地声明它的XMLName. 就像是type CommandResult struct {&nbsp; &nbsp; XMLName xml.Name `xml:"epp"`&nbsp; &nbsp; Result&nbsp; struct {&nbsp; &nbsp; &nbsp; &nbsp; Code&nbsp; &nbsp; int&nbsp; &nbsp; `xml:"code,attr" json:"code"`&nbsp; &nbsp; &nbsp; &nbsp; Message string `xml:"msg" json:"msg"`&nbsp; &nbsp; } `xml:"response>result" json:"response"`}因为没有它 XML 编码器会产生类似 <SomeCommand><response>...</response></SomeCommand>使用完整示例更新package mainimport (&nbsp; &nbsp; "bufio"&nbsp; &nbsp; "encoding/xml"&nbsp; &nbsp; "log")type Command interface {&nbsp; &nbsp; IsError() bool&nbsp; &nbsp; Request(buf *bufio.Writer, params interface{}) error}// Result of requesttype CommandResult struct {&nbsp; &nbsp; XMLName xml.Name `xml:"epp"`&nbsp; &nbsp; Result&nbsp; struct {&nbsp; &nbsp; &nbsp; &nbsp; Code&nbsp; &nbsp; int&nbsp; &nbsp; `xml:"code,attr" json:"code"`&nbsp; &nbsp; &nbsp; &nbsp; Message string `xml:"msg" json:"msg"`&nbsp; &nbsp; } `xml:"response>result" json:"response"`}// this Command's func is realized by CommandResultfunc (self CommandResult) IsError() bool {&nbsp; &nbsp; return true}// some commandtype SomeCommand struct {&nbsp; &nbsp; CommandResult}// this Command's func is realized by SomeCommandfunc (self SomeCommand) Request(buf *bufio.Writer, params interface{}) error {&nbsp; &nbsp; return nil}type AnotherCommand struct {&nbsp; &nbsp; CommandResult}func (self AnotherCommand) Request(buf *bufio.Writer, params interface{}) error {&nbsp; &nbsp; return nil}func main() {&nbsp; &nbsp; var c Command&nbsp; &nbsp; c = SomeCommand{}&nbsp; &nbsp; log.Println(c.IsError())&nbsp; &nbsp; c = AnotherCommand{}&nbsp; &nbsp; log.Println(c.IsError())}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go