gpt4 book ai didi

go - 棘手的 Go xml.Unmarshal() 案例

转载 作者:IT王子 更新时间:2023-10-29 00:43:53 26 4
gpt4 key购买 nike

我正在尝试在 Go 中像这样解码 XML:

<property>
<code value="abc"/>
<valueBoolean value="true"/>
</property>

或者这个

<property>
<code value="abc"/>
<valueString value="apple"/>
</property>

或者这个

<property>
<code value="abc"/>
<valueDecimal value="3.14159"/>
</property>

等等,变成这样:

type Property struct {
Code string `xml:"code>value,attr"`
Value interface{}
}

标记(valueBooleanvalueString 等)告诉我值属性的类型是什么。我试图解析的 XML 是 an international standard 的一部分,所以我无法控制它的定义。实现解析这些东西并不难,比如:

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)
...
}

但我不知道如何告诉 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 float `xml:"valueDecimal>value,attr"`
}

但这非常低效,特别是考虑到我将拥有这些东西的大量实例,这让我无法在不添加另一个属性来指示类型的情况下派生类型。

我能否以某种方式将它与普通的 XML 解码方法联系起来,只是手动处理棘手的部分,还是我需要从头开始为这种类型编写整个解码程序?

最佳答案

感谢 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

关于go - 棘手的 Go xml.Unmarshal() 案例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36939874/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com