douqianzha6213 2016-03-15 15:23
浏览 25
已采纳

如何在Go xml结构标记中表示替代项

I'm trying to write the appropriate set of struct tags to parse the XML version of UCUM. Following are two examples of the unit tag:

<unit Code="deg" CODE="DEG" isMetric="no" class="iso1000">
   <name>degree</name>
   <printSymbol>&#176;</printSymbol>
   <property>plane angle</property>
   <value Unit="[pi].rad/360" UNIT="[PI].RAD/360" value="2">2</value>
</unit>

<unit Code="[degF]" CODE="[DEGF]" isMetric="no" isSpecial="yes" class="heat">
   <name>degree Fahrenheit</name>
   <printSymbol>&#176;F</printSymbol>
   <property>temperature</property>
   <value Unit="degf(5 K/9)" UNIT="DEGF(5 K/9)">
      <function name="degF" value="5" Unit="K/9"/>
   </value>
</unit>

The tricky part is the contents of the value tag, which can either be a string (which I'd represent with a string attribute) or a function (which would require a struct of its own). Here's what I've got so far:

type Unit struct {
    Code             string `xml:Code,attr`
    CodeCaps         string `xml:CODE,attr`
    IsMetric         bool   `xml:isMetric,attr,omitempty`
    IsSpecial        bool   `xml:isEmptySpecial,attr,omitempty`
    Class            string `xml:class,attr`
    Name             string `xml:name`
    PrintSymbol      string `xml:printSymbol,chardata`
    DimensionTypeKey string `xml:property,chardata`
    Value            struct {
        Unit       string `xml:Unit,attr`
        UnitCaps   string `xml:UNIT,attr`
        Value      string `xml:Value,attr`
        PrintValue string `xml:,chardata`
        Function   struct { ... }
    } `xml:value`
}

How do I accurately describe this XML with struct tags?

  • 写回答

1条回答 默认 最新

  • doupacan2098 2016-03-15 16:47
    关注

    Your code is almost working; here's the fixed version.

    Basically you need the following adjustments:

    • In struct tags, the values—bits after the xml: prefix—must be enclosed in double quotes, like this:

      `xml:"foo,attr"`
      
    • There's no need to specify the ,chardata bit when you already just want to get whatever is between the element's tags.

    • To extract the <function> element just provide a data type for is: the parser will extract it if it's present or not if it's absent.

      To tell whether there was <function> or not, you can check the value of the PrintValue field: if it's not all-whitespace, there was no <function> element; otherwise there was.

      Alternatively, define a separate struct data type for unmarshaling this element, and have the field for it defined as a pointer to that type, like in

      type Function struct { ... }
      ...
      Function *Function `xml:"function"`
      

      This way, if there was no <function> element the field's value will be nil, otherwise it will point to a heap-allocated Function instance.

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

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘