英文:
Tricky Go xml.Unmarshal() case
问题
我正在尝试在Go中解组类似以下的XML:
<property>
<code value="abc"/>
<valueBoolean value="true"/>
</property>
或者这样的XML:
<property>
<code value="abc"/>
<valueString value="apple"/>
</property>
或者这样的XML:
<property>
<code value="abc"/>
<valueDecimal value="3.14159"/>
</property>
等等,将其解组成以下结构:
type Property struct {
Code string `xml:"code>value,attr"`
Value interface{}
}
其中标签(valueBoolean
,valueString
等)告诉我值属性的类型。我正在尝试解析的XML是国际标准的一部分,所以我无法控制其定义。实现解析这些内容不难,类似于:
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.ParseInt(value, 10, 64)
case "valueDecimal":
property.Value, err = strconv.ParseFloat(value, 64)
...
}
但是我无法弄清楚如何告诉XML包去找到它,而且这些内容被嵌套在其他XML中,我真的希望使用xml.Unmarshal
来处理。或者,我可以重新定义类型为:
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 float64 `xml:"valueDecimal>value,attr"`
}
但这样效率很低,特别是考虑到我将有大量这些实例,并且这样做没有办法在不添加另一个属性来指示类型的情况下推导出类型。
我是否可以将其与常规的XML解组方法结合起来,只需手动处理复杂部分,还是需要从头编写整个解组器来处理此类型?
英文:
I'm trying to unmarshal XML like this in Go:
<property>
<code value="abc"/>
<valueBoolean value="true"/>
</property>
or this
<property>
<code value="abc"/>
<valueString value="apple"/>
</property>
or this
<property>
<code value="abc"/>
<valueDecimal value="3.14159"/>
</property>
etc., into this:
type Property struct {
Code string `xml:"code>value,attr"`
Value interface{}
}
where the tag (valueBoolean
, valueString
, etc.) tells me what the type of the value attribute is. The XML that I'm trying to parse is part of an international standard, so I don't have any control over its definition. It wouldn't be hard to implement parsing these things, something like:
var value string
for a := range se.Attr {
if a.Name.Local == "value" {
value = a.Value
} else {
// Invalid attribute
}
}
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 figure out how to tell the XML package to find it, and these things are buried in other XML that I'd really rather use xml.Unmarshal
to handle. Alternately, I could redefine the type as:
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"`
}
but that's pretty inefficient, particularly given that I'll have a large number of instances of these things, and this leaves me no way to derive the type without adding another attribute to indicate the type.
Can I somehow tie this into the normal XML unmarshalling method, just handling the tricky part by hand, or do I need to write the whole unmarshaller for this type from scratch?
答案1
得分: 5
感谢OneOfOne的指导,这是一个与标准XML解组器很好配合的实现:
package main
import (
"encoding/xml"
"fmt"
"strconv"
"strings"
)
type Property struct {
Code string `xml:"code"`
Value interface{}
}
const xmldata = `<properties>
<property>
<code value="a"/>
<valueBoolean value="true"/>
</property>
<property>
<code value="b"/>
<valueString value="apple"/>
</property>
<property>
<code value="c"/>
<valueDecimal value="3.14159"/>
</property>
</properties>`
func (p *Property) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
if start.Name.Local != "property" {
return fmt.Errorf("Invalid start tag for Property")
}
for {
tok, err := d.Token()
if tok == nil {
break
}
if err != nil {
return err
}
switch se := tok.(type) {
case xml.StartElement:
var value string
var valueAssigned bool
for _, attr := range se.Attr {
if attr.Name.Local == "value" {
value = attr.Value
valueAssigned = true
} else {
return fmt.Errorf("Invalid attribute %s", attr.Name.Local)
}
}
if !valueAssigned {
return fmt.Errorf("Valid attribute missing")
}
switch se.Name.Local {
case "code":
p.Code = value
case "valueBoolean":
if value == "true" {
p.Value = true
} else if value == "false" {
p.Value = false
} else {
return fmt.Errorf("Invalid string %s for Boolean value", value)
}
case "valueString", "valueCode", "valueUri":
p.Value = value
case "valueInteger":
if ival, err := strconv.ParseInt(value, 10, 32); err != nil {
return err
} else {
p.Value = ival
}
case "valueDecimal":
if dval, err := strconv.ParseFloat(value, 64); err != nil {
return err
} else {
p.Value = dval
}
default:
return fmt.Errorf("Invalid tag %s for property", se.Name.Local)
}
}
}
return nil
}
func main() {
r := strings.NewReader(xmldata)
type Properties struct {
List []Property `xml:"property"`
}
var properties Properties
d := xml.NewDecoder(r)
if err := d.Decode(&properties); err != nil {
fmt.Println(err.Error())
}
for _, p := range properties.List {
switch p.Value.(type) {
case bool:
if p.Value.(bool) {
fmt.Println(p.Code, "is true")
} else {
fmt.Println(p.Code, "is false")
}
default:
fmt.Println(p.Code, "=", p.Value)
}
}
}
输出结果为:
a is true
b = apple
c = 3.14159
英文:
Thanks to the pointer from OneOfOne, here's an implementation that works well with the standard XML unmarshaler:
package main
import (
"encoding/xml"
"fmt"
"strconv"
"strings"
)
type Property struct {
Code string `xml:"code"`
Value interface{}
}
const xmldata = `<properties>
<property>
<code value="a"/>
<valueBoolean value="true"/>
</property>
<property>
<code value="b"/>
<valueString value="apple"/>
</property>
<property>
<code value="c"/>
<valueDecimal value="3.14159"/>
</property>
</properties>
`
func (p *Property) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
if start.Name.Local != "property" {
return fmt.Errorf("Invalid start tag for Property")
}
for {
tok, err := d.Token()
if tok == nil {
break
}
if err != nil {
return err
}
switch se := tok.(type) {
case xml.StartElement:
var value string
var valueAssigned bool
for _, attr := range se.Attr {
if attr.Name.Local == "value" {
value = attr.Value
valueAssigned = true
} else {
return fmt.Errorf("Invalid attribute %s", attr.Name.Local)
}
}
if !valueAssigned {
return fmt.Errorf("Valid attribute missing")
}
switch se.Name.Local {
case "code":
p.Code = value
case "valueBoolean":
if value == "true" {
p.Value = true
} else if value == "false" {
p.Value = false
} else {
return fmt.Errorf("Invalid string %s for Boolean value", value)
}
case "valueString", "valueCode", "valueUri":
p.Value = value
case "valueInteger":
if ival, err := strconv.ParseInt(value, 10, 32); err != nil {
return err
} else {
p.Value = ival
}
case "valueDecimal":
if dval, err := strconv.ParseFloat(value, 64); err != nil {
return err
} else {
p.Value = dval
}
default:
return fmt.Errorf("Invalid tag %s for property", se.Name.Local)
}
}
}
return nil
}
func main() {
r := strings.NewReader(xmldata)
type Properties struct {
List []Property `xml:"property"`
}
var properties Properties
d := xml.NewDecoder(r)
if err := d.Decode(&properties); err != nil {
fmt.Println(err.Error())
}
for _, p := range properties.List {
switch p.Value.(type) {
case bool:
if p.Value.(bool) {
fmt.Println(p.Code, "is true")
} else {
fmt.Println(p.Code, "is false")
}
default:
fmt.Println(p.Code, "=", p.Value)
}
}
}
Output is:
a is true
b = apple
c = 3.14159
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论