I am trying to untie XML like this in Go:
<property>
<code value="abc"/>
<valueBoolean value="true"/>
</property>
or
<property>
<code value="abc"/>
<valueString value="apple"/>
</property>
or
<property>
<code value="abc"/>
<valueDecimal value="3.14159"/>
</property>
etc., in this:
type Property struct {
Code string `xml:"code>value,attr"`
Value interface{}
}
where the tag ( valueBoolean, valueStringetc.) tells me what the value attribute type is. The XML I'm trying to parse is part of an international standard , so I cannot control its definition. It would not be easy to parse these things, for example:
var value string
for a := range se.Attr {
if a.Name.Local == "value" {
value = a.Value
} else {
}
}
switch se.Name.Local {
case "code":
case "valueBoolean":
property.Value = value == "true"
case "valueString":
property.Value = value
case "valueInteger":
property.Value, err = strconv.ParseInteger(value)
case "valueDecimal":
property.Value, err = strconv.ParseFloat(value)
...
}
but I can’t understand how to tell the XML package to find it, and these things are buried in another XML, which I would rather use xml.Unmarshalfor processing. Alternatively, I could override the type as follows:
type Property struct {
Code string `xml:"code>value,attr"`
ValueBoolean bool `xml:"valueBoolean>value,attr"`
ValueString string `xml:"valueString>value,attr"`
ValueInteger int `xml:"valueInteger>value,attr"`
ValueDecimal float `xml:"valueDecimal>value,attr"`
}
, , , .
- XML-, unmarshaller ?