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 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line