亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

自定義xml解碼器問題

自定義xml解碼器問題

Go
米琪卡哇伊 2021-08-23 14:59:42
我有多個測試用例通過,但是這個失敗了。我在這里遺漏了什么導致解碼器錯誤地讀取我的目標鍵的內容?const respGenericFault1 = `<?xml version='1.0' encoding='UTF-8'?><SOAP-ENV:Envelope  xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"  xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"  xmlns:xsd="http://www.w3.org/1999/XMLSchema">   <SOAP-ENV:Body>     <SOAP-ENV:Fault>         <faultcode xsi:type="xsd:string">SOAP-ENV:Client</faultcode>         <faultstring xsi:type="xsd:string">Failed to validate</faultstring>     </SOAP-ENV:Fault>  </SOAP-ENV:Body></SOAP-ENV:Envelope>`type Fault struct {    FaultCode, FaultString string}func (f Fault) Error() string {    return "Fault Code: '" + f.FaultCode + "' FaultString: '" + f.FaultString + "'"}func ParseFault(b []byte) error {    reader := bytes.NewReader(b)    d := xml.NewDecoder(reader)    var start xml.StartElement    fault := Fault{}    found := false    // iterate through the tokens    for {        tok, _ := d.Token()        if tok == nil {            break        }        // switch on token type        switch t := tok.(type) {        case xml.StartElement:            start = t.Copy()            fmt.Println(start.Name.Local)        case xml.CharData:            key := strings.ToLower(start.Name.Local)            // fault was found, capture the values and mark as found            if key == "faultcode" {                found = true                fault.FaultCode = string(t)                fmt.Printf("%#v\n", string(t))            } else if key == "faultstring" {                found = true                fault.FaultString = string(t)            }        }    }    if found {        return fault    }    return nil}func main() {    err := ParseFault([]byte(respGenericFault1))    fmt.Printf("%#v\n", err)}這是游樂場網址:http : //play.golang.org/p/7PFPNsmast
查看完整描述

1 回答

  • 1 回答
  • 0 關注
  • 173 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號