如何使用不规则属性封送和解封XML

问题描述 投票:0回答:2

我有一个XML,其中不同的实体用相同的标签表示但具有不同的属性。像这样的东西:

<xml>
    <body>
        <Packet type="clients">
            <Row id="123" name="bill"/>
            <Row id="456" name="sasha"/>
        </Packet>
        <Packet type="orders">
            <Row sum="90" status="DONE" />
            <Row sum="190" status="DONE" />
       </Packet>
   </body>
</xml>

保证每种类型只有一个包。

不幸的是,更改XML结构不在我的职责范围之内,我必须按原样使用它。在Go中处理这种结构的最佳方法是什么?我应该拥有一个具有所有可能属性的结构,并为每个“数据包”填充正确的结构吗?]

type XML struct {
    XMLName xml.Name `xml:"xml"`
    Body    struct {
        Packet []struct {
            Type string `xml:"type,attr"`
            Row  []struct {
                ID     string `xml:"id,attr"`
                Name   string `xml:"name,attr"`
                Sum    string `xml:"sum,attr"`
                Status string `xml:"status,attr"`
            } `xml:"Row"`
        } `xml:"Packet"`
    } `xml:"body"`
} 

这似乎可行,但是非常不方便,因为实际上我有许多不同类型的数据包,它们具有不同的属性负载。

或者也许有一种方法可以将数据包映射到不同的Go结构?像这样:

type XML struct {
    XMLName xml.Name `xml:"xml"`
    Body    struct {
        ClientPacket struct {
            Type string `xml:"type,attr"`
            Row  []struct {
                ID   string `xml:"id,attr"`
                Name string `xml:"name,attr"`
            } `xml:"Row"` 
        } `xml:"Packet"` // ???
        OrderPacket struct {
            Type string `xml:"type,attr"`
            Row  []struct {
                Sum   string `xml:"sum,attr"`
                Status string `xml:"status,attr"`
            } `xml:"Row"` 
        } `xml:"Packet"` // ???
    } `xml:"body"`
} 

后面的例子看起来更聪明,可读性也更高,但是我不理解如何使用它来编组和解组xml。

xml go attributes marshalling unmarshalling
2个回答
0
投票

我可以想到几种实现此目的的方法:您可以尝试使用已经描述的方法,并使用包含所有可能属性的结构。这将是乏味但容易的。您也可以尝试为Packet编写一个自定义拆组器:

type PacketXML struct {
   A *PacketTypeA
   B *PacketTypeB
   ...
}

func (x *PacketXML)  UnmarshalXML(d *Decoder, start StartElement) error {
    // Look at start attributes, find out what type of packet it has
    packetType:=findAttr(start,"type")
    switch packetType {
      case "typeA":
         x.A=&PacketTypeA{}
         return d.Decode(x.A)
      case "typeB":
         x.B=&PacketTypeB{}
         return d.Decode(x.B)
      ...
    }
  return nil
}

0
投票

[封送之前先转换文件(例如,使用XSLT)。

© www.soinside.com 2019 - 2024. All rights reserved.