doulan4371 2019-07-03 09:59
浏览 80
已采纳

如何在GO中解组动态XML

I try to parse XML using Go, which is used to exchange data with another system. I am aware how parsing with the xml.Unmarshal function works in principle. However, in the protocol the child element of the root element <PROTO> vary (see the examples below). For the child elements (REQUEST.DATA, REQUEST.ACL, REQUEST.NAC, ... - far more than twenty different) I have defined structs with xml:...-syntax so far - the parsing of these single child elements works.

But, is there a nice way to parse the whole message as a struct, where one varible type depends on the child element? For example with using an interface?

Example of possible XML messages:

<PROTO
   version="1.00">
  <REQUEST.DATA>
    <DATA-L>Some information</DATA-L>
  </REQUEST.DATA>
</PROTO>
<PROTO
   version="1.00">
  <REQUEST.ACK>
    <ACK-ID>1</ACK-ID>
  <REQUEST.ACK>
</PROTO>

Snippets from my Go application:

// XML: REQUEST.DATA
type DataRequest struct {
    LData string `xml:"DATA-L"`
}

// XML: REQUEST.ACK
type AckRequest struct {
    AckId int `xml:"ACK-ID"`
}

// XML: PROTO  <============= ??
type Request struct {
    Version float32  `xml:"version,attr"`
    RemoteRequest {AckRequest, DataRequest, ...} ????
}

func main() {
    message := `<PROTO version="1.00"><REQUEST.ACK><ACK-ID>1</ACK-ID><REQUEST.ACK></PROTO>`
    data := `<REQUEST.ACK><ACK-ID>1</ACK-ID><REQUEST.ACK>`
    doc := &AckRequest{}
    err := xml.Unmarshal([]byte(data), &doc)
    if err != nil {
        fmt.Printf("error: %v", err)
        return
    }
    fmt.Printf("data %+v", doc)
}

Instead of just parsing the child element data I want to parse the whole document message (<PROTO> involved). And want have a structure that contains the information of the respective child element.

  • 写回答

1条回答 默认 最新

  • duanmao2650 2019-07-03 10:16
    关注

    You can embed the DataRequest and AckRequest as pointers in the Request struct. This way you can check later if they are nil or not.

    // XML: REQUEST.DATA
    type DataRequest struct {
        LData string `xml:"DATA-L"`
    }
    
    // XML: REQUEST.ACK
    type AckRequest struct {
        AckId int `xml:"ACK-ID"`
    }
    
    type Request struct {
        Version float32      `xml:"version,attr"`
        Ack     *AckRequest  `xml:"REQUEST.ACK"`
        Data    *DataRequest `xml:"REQUEST.DATA"`
    }
    
    func main() {
        message := `<PROTO version="1.00"><REQUEST.ACK><ACK-ID>1</ACK-ID></REQUEST.ACK></PROTO>`
        proto := &Request{}
        err := xml.Unmarshal([]byte(message), &proto)
        if err != nil {
            fmt.Printf("error: %v", err)
            return
        }
        // if proto.Ack == nil {
        //   fmt.Println("Ack is nil")
        // }
        fmt.Printf("data %+v
    ", proto)
        fmt.Printf("data %+v
    ", proto.Ack)
    }
    

    data &{Version:1 Ack:0xc0000b6050 Data:}

    data &{AckId:1}

    You can also implement your own decoder and do dynamic type switching https://stackoverflow.com/a/33517875/1199408.

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

报告相同问题?

悬赏问题

  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型